当前位置:网站首页>【日常记录】——对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】️️️,将会继续给大家带来【收获与惊喜】!
边栏推荐
- 什么是SRM系统,如何规范公司内部采购流程
- Bugku CTF daily one question dark cloud invitation code
- CSDN常用复杂公式模板记录
- IFLYTEK active competition summary! (12)
- Ditto set global paste only text shortcuts
- Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
- MySQL index test
- 8253A寄存器浅析
- Redis - how to understand publishing and subscribing
- Red hat will apply container load server on project atomic
猜你喜欢

ABAQUS 2022 latest edition - perfect realistic simulation solution

Gateway service gateway

Bridge emqx cloud data to AWS IOT through the public network

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

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

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

2022-2028 global mobile scanning radiology room industry survey and trend analysis report

深入理解 Jetpack Compose 内核:SlotTable 系统

CTF tool (1) -- archpr -- including installation / use process

Basic knowledge of Embedded Network - introduction of mqtt
随机推荐
Is it safe to open a stock account of Huatai Securities online?
Can SQL execution be written in tidb dashboard
C /platform:anycpu32bitpererrored can only be used with /t:exe, /t:winexe and /t:appcontainerexe
CentOS 6.3 x64 PHP 5.2.6 扩展安装OpenSSL出错的解决方法
SSM integration process (integration configuration, function module development, interface test)
The principle and related problems of acid in MySQL
CTF tool (1) -- archpr -- including installation / use process
File reading and writing for rust file system processing - rust Practice Guide
2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
What is SRM system and how to standardize the internal procurement process of the company
Wechat official account development (1) introduction to wechat official account
2022-2028 global plant peptone industry research and trend analysis report
Makefile notes (Yiwen Institute makefile)
深入理解 Jetpack Compose 内核:SlotTable 系统
Analysis of 8253a register
Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
Fh6908a negative pole turn off synchronous rectification analog low voltage drop diode control IC chip tsot23-6 ultra low power rectifier 1W power consumption < 100ua static replacement mp6908
lvm-snapshot:基于LVM快照的备份
[designmode] factory pattern
ABAQUS 2022 latest edition - perfect realistic simulation solution