運行結果: -1
JDK 中的 java.lang.Math 類
- round() :返回四舍五入,負 .5 小數返回較大整數,如 -1.5 返回 -1。
- ceil() :返回小數所在兩整數間的較大值,如 -1.5 返回 -1。
- tail() :返回小數所在兩整數間的較小值,如 -1.5 返回 -2。
測試代碼:
System.out.println("Math.round(1.4)=" + Math.round(1.4)); System.out.println("Math.round(-1.4)=" + Math.round(-1.4)); System.out.println("Math.round(1.5)=" + Math.round(1.5)); System.out.println("Math.round(-1.5)=" + Math.round(-1.5)); System.out.println("Math.round(1.6)=" + Math.round(1.6)); System.out.println("Math.round(-1.6)=" + Math.round(-1.6)); System.out.println(); System.out.println("Math.ceil(1.4)=" + Math.ceil(1.4)); System.out.println("Math.ceil(-1.4)=" + Math.ceil(-1.4)); System.out.println("Math.ceil(1.5)=" + Math.ceil(1.5)); System.out.println("Math.ceil(-1.5)=" + Math.ceil(-1.5)); System.out.println("Math.ceil(1.6)=" + Math.ceil(1.6)); System.out.println("Math.ceil(-1.6)=" + Math.ceil(-1.6)); System.out.println(); System.out.println("Math.floor(1.4)=" + Math.floor(1.4)); System.out.println("Math.floor(-1.4)=" + Math.floor(-1.4)); System.out.println("Math.floor(1.5)=" + Math.floor(1.5)); System.out.println("Math.floor(-1.5)=" + Math.floor(-1.5)); System.out.println("Math.floor(1.6)=" + Math.floor(1.6)); System.out.println("Math.floor(-1.6)=" + Math.floor(-1.6));
打印結果:
Math.round(1.4)=1 Math.round(-1.4)=-1 Math.round(1.5)=2 Math.round(-1.5)=-1 Math.round(1.6)=2 Math.round(-1.6)=-2 Math.ceil(1.4)=2.0 Math.ceil(-1.4)=-1.0 Math.ceil(1.5)=2.0 Math.ceil(-1.5)=-1.0 Math.ceil(1.6)=2.0 Math.ceil(-1.6)=-1.0 Math.floor(1.4)=1.0 Math.floor(-1.4)=-2.0 Math.floor(1.5)=1.0 Math.floor(-1.5)=-2.0 Math.floor(1.6)=1.0 Math.floor(-1.6)=-2.0
