代码:
tempAvg[l] = tempSum[l].divide(new BigDecimal(daynum+""));异常
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.原因:
BigDecimal除法需要设定数的精度和舍入模式。
解决方案:
tempAvg[l] = tempSum[l].divide(new BigDecimal(daynum+""),2,BigDecimal.ROUND_HALF_UP);精度为2,舍入模式为四舍五入
参考资料https://stackoverflow.com/questions/4591206/arithmeticexception-non-terminating-decimal-expansion-no-exact-representable
本文介绍了一种在Java中使用BigDecimal进行除法运算时遇到的非终止十进制扩展异常,并提供了解决方案:通过设置精度和舍入模式来避免此类问题。
12万+

被折叠的 条评论
为什么被折叠?



