1. 程式人生 > 實用技巧 >Java 中的 Math. round(-1. 5) 等於多少?

Java 中的 Math. round(-1. 5) 等於多少?

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
	}
}