场景:两个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);

  

相关文章:

  • 2021-07-31
  • 2022-02-28
  • 2022-12-23
  • 2021-03-26
  • 2022-02-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-19
  • 2021-05-21
  • 2022-12-23
  • 2022-01-12
  • 2022-03-05
相关资源
相似解决方案