System.out.println("Result="+new BigDecimal(((63.19* 15) + (63.37* 5))).divide(new BigDecimal(15 + 5), MathContext.DECIMAL64).doubleValue());
Result=63.23499999999999
But with MathContext.DECIMAL32
we are getting correct result, see below:
System.out.println("Result="+new BigDecimal(((63.19* 15) + (63.37* 5))).divide(new BigDecimal(15 + 5), MathContext.DECIMAL32).doubleValue());
Result=63.235
The problem here not
BigDecimal
, but the fact that(63.19* 15) + (63.37* 5)
is not1264.7
but1264.6999999999998
, because the former cannot be represented as adouble
.If you do
instead, you get the desired result.