Skip to content
标签
欢迎扫码关注公众号

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

BigDecimal 运算时发生了如下异常:

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

参考 Non-terminating decimal expansion; no exact representable decimal result 解决了该问题。

原因是结果是无限小数时,必须指定小数点后的精度,否则就报这个异常。

解决办法就是调用 divide 方法时指定小数点后的精度。

代码如下:

java
BigDecimal num3 = num1.divide(num2,10,BigDecimal.ROUND_HALF_DOWN);

PS : 感觉 java 这点做的很不友好,难道不是应该使用 BigDecimal 最大精度作为计算结果吗?

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.