I stumbled across following scenario where how rounding works does not make sense to me.
new BigDecimal(9999.005).setScale(2, RoundingMode.HALF_UP) -- 9999.0
new BigDecimal(999.005).setScale(2, RoundingMode.HALF_UP) -- 999.0
new BigDecimal(9.005).setScale(2, RoundingMode.HALF_UP) -- 9.01
Math.round(9999.005*100.00)/100.000 -- 9999.0
Math.round(999.005*100.00)/100.000 -- 999.01
Math.round(9.005*100.00)/100.000 -- 9.01
My understanding is when RoundingMode.HALF_UP is used, it should convert all three values .01 regardless of what is the number before decimal.
Java documentation - Rounding mode to round towards {@literal "nearest neighbor"} * unless both neighbors are equidistant, in which case round up. * Behaves as for {@code RoundingMode.UP} if the discarded * fraction is ≥ 0.5; otherwise, behaves as for * {@code RoundingMode.DOWN}. Note that this is the rounding * mode commonly taught at school.
Am I wrong in the understanding on how this should work?