Search code examples
javabigdecimal

Why does BigDecimal divide cause rounding when it should fit within the significant digits?


Why does BigDecimal divide cause rounding to happen even though the value should fit comfortably in 4 significant digits?

BigDecimal realPercent = new BigDecimal(1.25, new MathContext(4));
// 1.25
BigDecimal percentDivider = new BigDecimal(100, new MathContext(4));
realPercent = realPercent.divide(percentDivider, BigDecimal.ROUND_HALF_UP);
// 0.01

It's possible to make divide behave like I want by setting the precision on the method call, but it doesn't explain why it doesn't already do it.

realPercent.divide(percentDivider, 4, BigDecimal.ROUND_HALF_UP)
// 0.0125

Solution

  • From the Javadoc of divide(BigDecimal, RoundingMode):

    Returns a BigDecimal whose value is (this / divisor), and whose scale is this.scale()

    The last part is important - the scale of your initial value is 2 (the unscaled value is 125). Because you've called an overload that pins the scale, the closest result available is scale=2, unscaled=1, i.e. 0.01.