BigDecimal, division & MathContext - very strange behaviour
Posted
by blackliteon
on Stack Overflow
See other posts from Stack Overflow
or by blackliteon
Published on 2010-03-27T13:59:18Z
Indexed on
2010/03/27
14:03 UTC
Read the original article
Hit count: 296
CentOs 5.4, OpenJDK Runtime Environment (build 1.6.0-b09)
MathContext context = new MathContext(2, RoundingMode.FLOOR);
BigDecimal total = new BigDecimal("200.0", context);
BigDecimal goodPrice = total.divide(BigDecimal.valueOf(3), 2, RoundingMode.FLOOR);
System.out.println("divided price=" + goodPrice.toPlainString());
// prints 66.66
BigDecimal goodPrice2 = total.divide(BigDecimal.valueOf(3), new MathContext(2, RoundingMode.FLOOR));
System.out.println("divided price2=" + goodPrice2.toPlainString());
// prints 66
BUG ?
© Stack Overflow or respective owner