Java基礎
Java 中的 Math. round(-1. 5) 等於多少?
Math.round(-1.5)的返回值是-1。四舍五入的原理是在參數上加0.5然后做向下取整。
它有三個特例:
1.如果參數為 NaN(無窮與非數值) ,那么結果為 0。
2.如果參數為負無窮大或任何小於等於 Long.MIN_VALUE 的值,那么結果等於Long.MIN_VALUE 的值。
3.如果參數為正無窮大或任何大於等於 Long.MAX_VALUE 的值,那么結果等於Long.MAX_VALUE 的值。
public class test {
public static void main(String[] args){
System.out.println(Math.round(1.3)); //1
System.out.println(Math.round(1.4)); //1
System.out.println(Math.round(1.5)); //2
System.out.println(Math.round(1.6)); //2
System.out.println(Math.round(1.7)); //2
System.out.println(Math.round(-1.3)); //-1
System.out.println(Math.round(-1.4)); //-1
System.out.println(Math.round(-1.5)); //-1
System.out.println(Math.round(-1.6)); //-2
System.out.println(Math.round(-1.7)); //-2
}
}
