Arithmeticexception: "Non-Terminating Decimal Expansion; No Exact Representable Decimal Result"

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

From the Java 11 BigDecimal docs:

When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.)

As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3.

If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

To fix, you need to do something like this:

a.divide(b, 2, RoundingMode.HALF_UP)

where 2 is the scale and RoundingMode.HALF_UP is rounding mode

For more details see this blog post.

Error while dividing BigDecimal value : java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result

The answer was in that post, and stated multiple time also:

a.divide(b, 8, RoundingMode.HALF_EVEN);

try this for your divide statement, (the scale is 8 for your precision).
What you're doing right now is dividing two large decimals, which results in an ultra large decimal answer, and you're not controlling the precision.

What causes Non-terminating decimal expansion exception from BigDecimal.divide?

Non-terminating decimal need rounding

When using divide you should use a MathContext with RoundingMode in case the exact result has an infinite number of decimals.

Such is your case:

MathContext mc = new MathContext(2, RoundingMode.HALF_UP) ;
BigDecimal bd3 = bd1.divide(bd2, mc);

Alternatively call divide with a rounding mode to use the scale of the numerator (bd1 in the example below):

BigDecimal bd3 = bd1.divide(bd2, RoundingMode.HALF_UP);

Java run-time error Non-terminating decimal expansion; no exact representable decimal result in BigDecimal class

You probably need to use one of the divide methods that specify the rounding mode.

Read also here.

Java BigDecimal divide

The ArithmeticException is thrown because your division leads to a non terminating decimal, if you explicitly provide the method with a rounding mode, this exception will no longer be thrown. So, try this

BigDecimal average = total.divide(test_count,  RoundingMode.HALF_UP);

Why Am I Getting a Non-terminating decimal expansion when I am rounding up already?

Tell BigDecimal how you want to round when you do the divide. Like this:

BigDecimal TotalPackageInt=  cartQty.divide(cartMultiplier, 0, RoundingMode.UP );


Related Topics



Leave a reply



Submit