为什么以下代码会引发如下所示的异常?
BigDecimal a = new BigDecimal("1.6");
BigDecimal b = new BigDecimal("9.2");
a.divide(b) // results in the following exception.
例外:
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
原文由 Jason 发布,翻译遵循 CC BY-SA 4.0 许可协议
从 Java 11
BigDecimal
文档:要修复,您需要执行以下操作:
其中 2 是比例,RoundingMode.HALF_UP 是舍入模式
有关更多详细信息,请参阅 此博客文章。