当前位置:网站首页>【日常记录】——对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);
边栏推荐
- Next. JS hot update markdown file change
- Sentinel哨兵机制
- Use diskgenius to expand the capacity of system disk C
- 中断操作:AbortController学习笔记
- 学习太极创客 — MQTT(二)MQTT 基本原理
- jupyter的使用
- 服务器四通道内存性能提升,四通道内存性能怎么样
- Sword finger offer II 010 Subarray prefix sum difference with sum K
- Precautions for using timer_cap.c of Jerry [chapter]
- 智能对话01-redis的安装
猜你喜欢

超全金屬PBR多通道貼圖素材網站整理
![[matlab] numerical calculus and equation solving](/img/4a/4eca552bd0d2aa71f8b35d92eb9bb7.png)
[matlab] numerical calculus and equation solving

Distributed remote management of distribution room environment

【编译原理】概述

杰理之系统时钟设置出现复位或无效问题【篇】

Comprehensive optimization of the six topics, Alibaba performance optimization booklet open source, leading you to the ultimate performance

Mathematical modeling - linear programming

数学建模——非线性规划

DDD concept is complex and difficult to understand. How to design code implementation model in practice?
![[UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update](/img/22/0c13e98e634a99d1680dd4bb12eaef.png)
[UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update
随机推荐
Mathematical modeling -- integer programming
Vscode automatically generates ifndef define ENDIF of header file
[efficiency] another note artifact is open source!
Mathematical modeling - linear programming
有关均衡----简易版瓶颈模型
A development of student management system based on PHP
ACY100油烟浓度在线监控仪针对饮食业厨房油烟排放
CGI connects to database through ODBC
大学生暑假换机热,ROG 明星产品幻 16 翻转版 / 幻 13 / 幻 X 预约
Huawei cloud gaussdb (for redis) unveiling issue 19: gaussdb (for redis) comprehensive comparison with CODIS
Precautions for the use of Jerry's wake-up mouth [chapter]
PSYNC command of redis
宝藏又小众的国画3d材质贴图素材网站分享
Mathematical modeling - nonlinear programming
SDN system method | 10 The future of SDN
golang sort slice int
BILSTM和CRF的那些事
Website arrangement of super all metal PBR multi-channel mapping materials
十大证券公司哪个佣金最低 办理开户安全吗
有关QueryInterface函数