Math.round???大侠们帮忙。

来源:百度知道 编辑:UC知道 时间:2024/06/04 23:24:26
public class Test {

/**
* @param args
*/
public static void main(String[] args) {

System.out.println(Math.round(-11.5));
System.out.println(Math.round(-12.4));
System.out.println(Math.round(-12.5));
System.out.println(Math.round(-12.6));
}
}

显示结果:
-11
-12
-12
-13

这些结果是怎么算出来的?
System.out.println(Math.round(-12.5));
System.out.println(Math.round(-12.6));

-12
-13
????
怎么结果不一样。
四舍六入五成双是什么意思???

round(参数)是最接近参数的整数是几,通俗讲就是四舍五入。
但是负数要注意,五入的时候要往大的数入。比如-12.5 就变-12而不是-13因为-12比-13大,
所以以上结果就算出来了

(long)Math.floor(a + 0.5d)
就是这么算出来的,有什么不明白的?
Math.floor(-11)
Math.floor(-11.9)
Math.floor(-12.0)
Math.floor(-12.1)
Math.floor(double a)
-11
-12
-12
-13

-11
-12
-12
-13
应该是这么多吧!!!
我比较同意二楼的意见,应该是四舍五入吧!!!

round是逢X.5就计算成X+1

四舍五入 但是负小数要改成负整数+正小数 如,-11.5=-12+0.5=-12+1 round(-11.5) 的值就是-11

round()的计算规则是:先加上0.5再取整.所以Math.round(11.5)=12;Math.round(-11.5)=-11