I have created the following helper function for rounding decimals:
public static double round(double value, int places, RoundingMode roundingMode) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, roundingMode);
return bd.doubleValue();
}
I am confused by the results I get when using RoundingMode.CEILING
:
round(2.0, 0, RoundingMode.CEILING); // returns 2, which is expected
round(2.180, 2, RoundingMode.CEILING); // returns 2.19, which seems odd as I expected 2.18
Could somebody please explain why in the second call, the value is effectively rounded up to 2.19?