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

Today, when debugging a java program, the following exception error message appears:

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

The error statements are as follows:

one

fun.divide(bar));

If you use BigDecimal to do Division in Java, you must pass the second parameter in the divide method, and the definition must be accurate to a few decimal places. Otherwise, if the result is infinite circular decimal without integer division, the above exception will be thrown
solutions

one

fun.divide(bar,

2

, BigDecimal.ROUND_ HALF_ DOWN);

Note that there are two overloaded methods for this divide method. One is to pass two parameters, and the other is to pass three parameters

Two parameter method:

@Param divisor value by which this {@ code BigDecimal} is to be divided

@Param rounding mode rounding mode to apply

The method of three parameters is as follows

@Param divisor value by which this {@ code BigDecimal} is to be divided. Incoming divisor
@ param scale of the {@ code BigDecimal} quotient to be returned

Similar Posts: