【发布时间】:2015-08-10 20:17:56
【问题描述】:
我想将十进制数四舍五入到最接近的自然数。 示例:
public static void main(String[] arguments){
BigDecimal a=new BigDecimal("2.5");
BigDecimal b=new BigDecimal("0.5");
System.out.println(a.round(new MathContext(1,RoundingMode.UP)));
System.out.println(b.round(new MathContext(1,RoundingMode.UP)));
}
预期输出
3
1
实际输出
3
0.5
问题是数字 0.5 舍入为 0.5 而不是 1 如何舍入小于 1 的 BigDecimal
【问题讨论】:
-
这是bug还是什么,还在想intvalue = intvalue.setScale(0, RoundingMode.UP);效果很好
-
是的,setScale(0) 有效,但我不知道为什么 BigDecimal 回合方法会如此有效。
标签: java rounding bigdecimal