当前位置:网站首页>[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 】!
边栏推荐
- Quick start of wechat applet -- project introduction
- 2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
- Get screen height
- [designmode] singleton pattern
- Rust controls Dajiang programmable UAV Tello
- Host FL Studio fruit music production daw20.9
- P4 learning - p4runtime
- HDU 2488 A Knight's Journey(DFS)
- [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
猜你喜欢
The programmer's girlfriend gave me a fatigue driving test
2022-2028 global electric yacht industry research and trend analysis report
Sword finger offer 19 Regular Expression Matching
Wechat official account development (1) introduction to wechat official account
剑指 Offer 18. 删除链表的节点
What should I do without 50W bride price
2022-2028 global 3D printing ASA consumables industry research and trend analysis report
Redis - understand the master-slave replication mechanism
PyTorch安装并使用gpu加速
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
随机推荐
leetcode 474. Ones and zeroes (medium)
PyTorch安装并使用gpu加速
leetcode 474. Ones and Zeroes 一和零(中等)
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
Integer to hexadecimal string PTA
New trend of embedded software development: Devops
BeanUtils. Copyproperties() vs. mapstruct
2022-2028 global herbal diet tea industry research and trend analysis report
解决 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.
Vulnerability discovery - App application vulnerability probe type utilization and repair
[UML] UML class diagram
MySQL index test
Tide - rust web framework based on async STD
MySQL variables, stored procedures and functions
Self examination before school starts
What is the fastest way to import data from HDFS to Clickhouse? Spark is imported through JDBC or HDFS
Length of the longest integrable subarray
第53章 从业务逻辑实现角度整体性理解程序
Sword finger offer 19 Regular Expression Matching
实验八 T-sql,存储过程