Financial Applications need precise and accurate calculations and this is achieved in Java using the java.math.BigDecimal class. While working with this class and using the *divide(BigDecimal divisor)* method I came across an issue which I wanted to describe here:

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

As per the Javadoc for BigDecimal this occurs since the ‘exact quotient cannot be represented’. This can be resolved by providing the scale (precision) and the RoundingMode to the divide() method. Something like this:

bdVar1.divide( bdVar2,6,RoundingMode.HALF_UP);

Where bdVar1 and bdVar2 are BigDecimals and 6 is the scale.

Hope this helps.

this post is very usefull thx!

Spot on with this write-up, I actually believe this site needs a great deal more attention. I’ll probably be returning to see

more, thanks for the information!