当前位置:网站首页>[daily record] - bug encountered in BigDecimal division operation
[daily record] - bug encountered in BigDecimal division operation
2022-07-01 00:54:00 【A brief memory of fleeting years】
hello ! Hello everyone , Today, I'd like to share with you an article about BigDecimal It occurs once during the division operation bug, I hope it will be helpful and rewarding to you .
Personal blog :https://blog.csdn.net/weixin_43759352
official account :【SimpleMemory】
️️️ If there is a pair of 【 back-end technology 】 Interested bosses , Welcome to your attention !!!️️️
Recently in the project test environment , Meet such a problem ; Check the log , The error information is as follows :
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1693)
Find... By positioning : Is used in code BigDecimal Of divide Method .
The inspection found that : This is because in the BigDecimal There is no error to divide the report , So we need to go to divide The function passes in parameter settings to keep several decimal places .
Problem reduction
Here is a test code to demonstrate the scenario without division .
BigDecimal a = new BigDecimal(1);
BigDecimal b = new BigDecimal(3);
BigDecimal result = a.divide(b);
System.out.println("result = " + result);
Treatment method
We treat it divide Method , Set the number of decimal places passed in by the parameter , And the specific rounding mode .
BigDecimal result = a.divide(b,2,BigDecimal.ROUND_HALF_UP); // Keep two decimal places 、 rounding
If this article 【 article 】 It will help you , I hope you will be more careful Fabulous 、 Comment on 、 Focus on 、 Collection ; If the 【 back-end technology 】 Interested little cute , You are welcome to pay attention to ️️️ official account 【SimpleMemory】️️️, Will continue to bring you 【 Harvest and surprise 】!
边栏推荐
- 20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
- JS bubble sort and select sort
- Search rotation sort array
- 2022-2028 global capsule shell industry research and trend analysis report
- What is product thinking
- 2022-2028 global mobile scanning radiology room industry survey and trend analysis report
- From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
- Error 2059 when Navicat connects to MySQL
- 解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
- Experiment 8 T-SQL, stored procedure
猜你喜欢

Mindjet mindmanager2022 mind map decompression installer tutorial

Solving the weird problem that the query conditions affect the value of query fields in MySQL query

Member management applet actual development 07 page Jump

2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report

Error 2059 when Navicat connects to MySQL

优质的水泵 SolidWorks模型素材推荐,不容错过

CentOS installation starts redis

C # Generate PPK files in Putty format (passthrough support)

Two-stage RO: part 1

Sword finger offer 18 Delete the node of the linked list
随机推荐
C # generates PPK files in putty format (supports passphrase)
leetcode 474. Ones and zeroes (medium)
Vulnerability discovery - App application vulnerability probe type utilization and repair
Rust book materials - yazhijia Library
Stack frame
1009 product of polynomials (25 points) [PTA class A]
Docsify building a personal minimalist knowledge warehouse
New trend of embedded software development: Devops
Error 2059 when Navicat connects to MySQL
When is it appropriate to replace a virtual machine with a virtual machine?
Yboj mesh sequence [Lagrange interpolation]
Thoughts on the future of data analysis in "miscellaneous talk"
Redis - cache penetration, cache breakdown, cache avalanche
Cmu15445 (fall 2019) project 1 - buffer pool details
2022-2028 global herbal diet tea industry research and trend analysis report
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
NE555波形发生器手把手教程之NE555内部结构(一)
Excuse me, does Flink support synchronizing data to sqlserver
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
Join table query select generation