当前位置:网站首页>【日常记录】——对BigDecimal除法运算时遇到的Bug
【日常记录】——对BigDecimal除法运算时遇到的Bug
2022-06-25 17:32:00 【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);
边栏推荐
- golang list to string
- About queryinterface functions
- Precautions for using Jerry's timer [chapter]
- Remote terminal control artifact - mobaxterm
- [black apple] Lenovo Savior y70002019pg0
- Sword finger offer II 035 Minimum time difference
- 杰理之获取复位源和唤醒的 IO 口的方法【篇】
- [UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update
- 【Matlab】数据插值
- [matlab] data interpolation
猜你喜欢

WARNING: Unsupported upgrade request.

Bilstm and CRF

UART波特率对时钟精度的要求有多高?
![Jerry's system clock setting is reset or invalid [chapter]](/img/c6/ee6b287af7d309f98abda8e11d674c.png)
Jerry's system clock setting is reset or invalid [chapter]

学习太极创客 — MQTT(一)MQTT 是什么

Solution to the problem of incorrect clock in FreeRTOS kernel

观察者模式之通用消息发布与订阅

Langage d'assemblage (5) Registre (accès à la mémoire)

ACY100油烟浓度在线监控仪针对饮食业厨房油烟排放

匯編語言(5)寄存器(內存訪問)
随机推荐
How does LSF see whether the job reserved slot is reasonable?
Can I open an account? Is it safe to open an account
杰理之定时器使用注意事项【篇】
Mathematical modeling - linear programming
IDEA全局搜索汉字[通俗易懂]
Website arrangement of super all metal PBR multi-channel mapping materials
[black apple] Lenovo Savior y70002019pg0
Win10开启热点共享后断网怎么解决?
Remote terminal control artifact - mobaxterm
国泰君安证券靠谱吗?是否合法?开股票账户安全吗?
Precautions for using Jerry's timer [chapter]
Can I open an account? Is it safe to open an account
C语言中%含义
Acy100 oil fume concentration online monitor for kitchen oil fume emission in catering industry
Kotlin入门(20)几种常见的对话框
container of()函数简介
Comprehensive optimization of the six topics, Alibaba performance optimization booklet open source, leading you to the ultimate performance
WARNING: Unsupported upgrade request.
数据挖掘之时间序列分析[通俗易懂]
用连续自然数之和来表达整数