当前位置:网站首页>[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 】!
边栏推荐
- Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
- P4学习——Basic Tunneling
- Line number of Jenkins pipeline script execution exception
- Redis - understand the master-slave replication mechanism
- Exercise and health
- [PHP] self developed framework qphp, used by qphp framework
- 20220215-ctf-misc-buuctf-einstein-binwalk analyze picture-dd command separate zip file -- look for password in picture attribute
- 获取屏幕高度
- 2022-2028 global elevator emergency communication system industry research and trend analysis report
- [DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
猜你喜欢
![[UML] UML class diagram](/img/6f/30bd15967103969e600d69e618d8bf.png)
[UML] UML class diagram

Ranger plug-in development (Part 2)

C#生成putty格式的ppk文件(支持passphrase)

NE555波形发生器手把手教程之NE555内部结构(一)

Multi graph explanation of resource preemption in yarn capacity scheduling

C language file operation for conquering C language

20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking

Confirm() method of window

2022-2028 global rampant travel industry research and trend analysis report

20220216 misc buuctf another world WinHex, ASCII conversion flag zip file extraction and repair if you give me three days of brightness zip to rar, Morse code waveform conversion mysterious tornado br
随机推荐
2022-2028 global mobile scanning radiology room industry survey and trend analysis report
Oracle临时表详解
Self examination before school starts
$watch will not trigger data change - $watch not firing on data change
Multi graph explanation of resource preemption in yarn capacity scheduling
Implementation of OSD on Hisilicon platform (1)
Mindjet mindmanager2022 mind map decompression installer tutorial
What should I do without 50W bride price
A detailed explanation of the implementation principle of go Distributed Link Tracking
Two-stage RO: part 1
The principle and related problems of acid in MySQL
JS bubble sort and select sort
ArrayList分析1-循环、扩容、版本
Longest valid bracket
Redis based distributed lock
Teach you how to use Hal library to get started -- become a lighting master
Join table query select generation
2022-2028 global encrypted external hard disk industry research and trend analysis report
CentOS install MySQL
Using Excel to quickly generate SQL statements