float naN = Float.NaN;
System.out.println("NaN is " + Math.round(naN));
System.out.println("10.46 is " + Math.round(10.46));
System.out.println("10.5 is " + Math.round(10.5));
System.out.println("10.66 is " + Math.round(10.66));
System.out.println("-10.46 is " + Math.round(-10.46));
System.out.println("-10.5 is " + Math.round(-10.5));
System.out.println("-10.66 is " + Math.round(-10.66));
输出结果为
NaN is 0
10.46 is 10
10.5 is 11
10.66 is 11
-10.46 is -10
-10.5 is -10
-10.66 is -11
从输出结果看
1.如果为 NaN,则返回 0;
2.如果为正数,小数部分大于等于5,则返回比正数大的值;
3.如果为负数,小数部分小于等于5,则返回比负数大的值;