BigDecimal除法

来源:互联网 发布:php wsdl 调用 编辑:程序博客网 时间:2024/05/16 09:45

代码:

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

原创粉丝点击