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:
Where bdVar1 and bdVar2 are BigDecimals and 6 is the scale.
Hope this helps.