当前位置:网站首页>[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 】!
边栏推荐
- 优质的水泵 SolidWorks模型素材推荐,不容错过
- Multi graph explanation of resource preemption in yarn capacity scheduling
- 运动与健康
- Problem solving: how to manage thread_local pointer variables
- Tibetan poem PTA
- File reading and writing for rust file system processing - rust Practice Guide
- Rust book materials - yazhijia Library
- [designmode] factory pattern
- CTF tool (1) -- archpr -- including installation / use process
- SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding
猜你喜欢

Can JDBC based on openjdk connect to MySQL?

Authentication principle of Ranger plug-in

CentOS install MySQL

初识 Flutter 的绘图组件 — CustomPaint

2022-2028 global ethylene oxide scrubber industry research and trend analysis report

2022-2028 global public address fire alarm system industry research and trend analysis report

P4学习——Basic Tunneling

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

SAP ui5 beginner tutorial 19 - SAP ui5 data types and complex data binding

第53章 从业务逻辑实现角度整体性理解程序
随机推荐
CSDN常用复杂公式模板记录
Redis based distributed lock
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Interface documentation system - Yapi
2022-2028 global elevator emergency communication system industry research and trend analysis report
P4学习——Basic Tunneling
IBL预计算的疑问终于解开了
Teach you how to use Hal library to get started -- become a lighting master
On the application of cluster analysis in work
Sword finger offer 18 Delete the node of the linked list
C#生成putty格式的ppk文件(支持passphrase)
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
Some views on libco
Bugku CTF daily one question dark cloud invitation code
ArrayList分析1-循环、扩容、版本
Date类的实现
20220215 CTF misc buuctf the world in the mirror the use of stegsolve tool data extract
Double linked list: initialize insert delete traversal
Luogu p1168 median
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!