Java BigDecimal setScale与RoundingMode.CEILING一起使用

时间:2017-09-30 11:04:38

标签: java math rounding bigdecimal

我为舍入小数创建了以下辅助函数:

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();
}

我对使用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

有人可以解释为什么在第二次通话中,该值实际上是向上舍入到2.19?

0 个答案:

没有答案