I've used BigDecimals before but not very often and I was working on something this morning and I kept getting the following exception:
Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(BigDecimal.java:1594)
I was attempting to set the scale and use rounding to eliminate the problem like so:
BigDecimal bd1 = new BigDecimal(1131).setScale(2,BigDecimal.ROUND_HALF_UP); BigDecimal bd2 = new BigDecimal(365).setScale(2,BigDecimal.ROUND_HALF_UP); BigDecimal bd3 = bd1.divide(bd2).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("result: " + bd3);
However, I keep getting the same exception. Anyone able to show me where I have made a mistake?
Non-Terminating, Non-Repeating Decimal. A non-terminating, non-repeating decimal is a decimal number that continues endlessly, with no group of digits repeating endlessly. Decimals of this type cannot be represented as fractions, and as a result are irrational numbers.
Example: 0.15, 0.86, etc. Non-terminating decimals are the one that does not have an end term. It has an infinite number of terms. Example: 0.5444444….., 0.1111111….., etc.
Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.
The largest value BigDecimal can represent requires 8 GB of memory.
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
passing the scale and rounding mode.
BigDecimal bd3 = bd1.divide(bd2, RoundingMode.HALF_UP);
Here's the problem
bd1.divide(bd2)
You need to use one of the overloaded divide()
methods that takes a rounding mode (in various forms) - you cannot do the rounding after the division because with a nonterminating fraction the intermediate result would either already need to be rounded, or require infinite storage space.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With