Wednesday, March 30, 2011

BigDecimal rounding issue

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


do this:


RoundingMode DEFAULT_ROUNDING_MODE = RoundingMode.HALF_UP;
int DIVIDE_SCALE = 10;
BigDecimal op1, op2
...
BigDecimal result = op1.divide(op2, DIVIDE_SCALE, DEFAULT_ROUNDING_MODE);


and you will be happy.

No comments: