当前位置:网站首页>【日常记录】——对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);
边栏推荐
- 杰理之adc_get_voltage 函数获取电压值不准【篇】
- Old mobile phones turn waste into treasure and serve as servers
- conda安装的py3.6和py3.7
- Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]
- Why do we need ankeri's active power filter in frequency converter occasions?
- Win10开启热点共享后断网怎么解决?
- golang sort slice int
- [UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update
- 超全金屬PBR多通道貼圖素材網站整理
- Best practices for data relocation: using CDM to relocate offline Mysql to DWS
猜你喜欢

智能对话01-redis的安装

WARNING: Unsupported upgrade request.

Mathematical modeling -- integer programming

Win10开启热点共享后断网怎么解决?

Super Full Metal PBR Multi - channel Mapping Materials website collation

Bilstm and CRF

杰理之如何给外界输出一个时钟源使用【篇】

Treasure and niche Chinese painting 3D texture material website sharing

杰理之获取复位源和唤醒的 IO 口的方法【篇】

汇编语言(5)寄存器(内存访问)
随机推荐
conda安装的py3.6和py3.7
The second round of Yunnan Cyberspace Security competition in May 2021
[matlab] data statistical analysis
Agent white paper - jointly build agents and create the wisdom of the whole scene | cloud library No.21 recommendation
求满足条件的最长子串长度
Can I open an account? Is it safe to open an account
Precautions for the use of Jerry's wake-up mouth [chapter]
Interrupt operation: abortcontroller learning notes
【Matlab】数据插值
[efficiency] another note artifact is open source!
Sword finger offer II 010 Subarray prefix sum difference with sum K
win10安装cuda的操作步骤(不断完美中)
Vscode / * * generate function comments
UART波特率对时钟精度的要求有多高?
Mathematical modeling - nonlinear programming
智能对话01-redis的安装
观察者模式之通用消息发布与订阅
HMS Core机器学习服务实现同声传译,支持中英文互译和多种音色语音播报
golang sort slice int
CONDA modifying a mirror source