当前位置:网站首页>【日常记录】——对BigDecimal除法运算时遇到的Bug
【日常记录】——对BigDecimal除法运算时遇到的Bug
2022-07-01 00:32:00 【浅殇忆流年】
哈喽!大家好,今天给大家分享一篇最近在项目中遇到BigDecimal除法运算的时候出现的一次bug,希望对大家有所帮助和收获。
个人博客:https://blog.csdn.net/weixin_43759352
公众号:【SimpleMemory】
️️️如果有对【后端技术】感兴趣的大佬们,欢迎关注!!!️️️
最近在项目测试环境中,遇到这样一个问题;查看日志,报错信息如下:
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1693)
通过定位发现:是在代码中使用BigDecimal 的divide方法时出现了异常。
检查发现:这是因为在BigDecimal 没有整除报的错误,因此我们需要向divide函数传入参数设置保留几位小数。
问题还原
下面通过一个测试代码演示没有整除的场景。
BigDecimal a = new BigDecimal(1);
BigDecimal b = new BigDecimal(3);
BigDecimal result = a.divide(b);
System.out.println("result = " + result);
处理办法
我们对其divide方法,设置参数传入几位小数,以及具体的取整模式即可。
BigDecimal result = a.divide(b,2,BigDecimal.ROUND_HALF_UP); // 保留两位小数、四舍五入
如果这篇【文章】对您有帮助,希望大家点赞、评论、关注、收藏;如果对【后端技术】感兴趣的小可爱,也欢迎关注️️️ 公众号【SimpleMemory】️️️,将会继续给大家带来【收获与惊喜】!
边栏推荐
- CentOS installation starts redis
- leetcode 474. Ones and Zeroes 一和零(中等)
- 2022-2028 global single travel industry research and trend analysis report
- JS bubble sort and select sort
- Quick start of wechat applet -- project introduction
- On the application of cluster analysis in work
- File reading and writing for rust file system processing - rust Practice Guide
- Can SQL execution be written in tidb dashboard
- Don't worry about whether you can be a coder if you don't learn English well. Learn it first
- Bridge emqx cloud data to AWS IOT through the public network
猜你喜欢

Software supply chain security risk pointing North for enterprise digitalization and it executives

SSM integration process (integration configuration, function module development, interface test)
![[untitled]](/img/96/7f26614bbdcce71006e38ee34ab216.jpg)
[untitled]

Random ball size, random motion collision

Why should VR panoramic shooting join us? Leverage resources to achieve win-win results

To tell you the truth, ThreadLocal is really not an advanced thing

什么是SRM系统,如何规范公司内部采购流程

What is SRM system and how to standardize the internal procurement process of the company

女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!

The programmer's girlfriend gave me a fatigue driving test
随机推荐
在指南针上买基金安全吗?
When is it appropriate to replace a virtual machine with a virtual machine?
Luogu p1144 shortest circuit count
Redis - understand the master-slave replication mechanism
Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)
In depth understanding of jetpack compose kernel: slottable system
LVM snapshot: preparation of backup based on LVM snapshot
The programmer's girlfriend gave me a fatigue driving test
Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
Luogu p1168 median
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
SSM integration process (integration configuration, function module development, interface test)
How to edit special effects in VR panorama? How to display detailed functions?
Combining online and offline, VR panorama is a good way to transform furniture online!
The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
8253A寄存器浅析
Red hat will apply container load server on project atomic
深入理解 Jetpack Compose 内核:SlotTable 系统
LVM snapshot: backup based on LVM snapshot
2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main()