BigDecimal得除法


场景:两个BigDecimal之间做除法

BigDecimal result = dto.getCargoWeight().divide(record.getCargoWeight()); 

问题:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

原因:BigDecimal类型得数据之间在做除法时,如果结果是无限循环小数,那么就会报上面得异常。

解决方法:找到divide得重载方法,让除法得结果保留小数位即可,我这边是选择了保留两位小数。

BigDecimal result = dto.getCargoWeight().divide(record.getCargoWeight(),2, BigDecimal.ROUND_HALF_UP);

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM