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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM