BigDecimal.setScale 工作有误
BigDecimal.setScale works wrong
这是我的舍入函数:
return new BigDecimal("4.780").setScale(2, RoundingMode.HALF_UP).doubleValue();
我认为结果应该是 4.8,但 returns 我是 4.78。
有人可以解释为什么吗?
正如 Javadoc 中所说:
If zero or positive, the scale is the number of digits to the right of the decimal point
所以,如果你设置为2,小数点右边会有2位。
这是我的舍入函数:
return new BigDecimal("4.780").setScale(2, RoundingMode.HALF_UP).doubleValue();
我认为结果应该是 4.8,但 returns 我是 4.78。 有人可以解释为什么吗?
正如 Javadoc 中所说:
If zero or positive, the scale is the number of digits to the right of the decimal point
所以,如果你设置为2,小数点右边会有2位。