1.Math.round用法
- java.lang.Math用於數學運算
- round() 四捨五入
- floor() 返回小於等於參數的最大整數
- ceil() 返回大於等於參數的最大整數
public class Math用法 {
public static void main(String[] args) {
System.out.println("Math.floor(3.55815)=" + Math.floor(3.55815));
System.out.println("Math.floor(-3.55815)=" + Math.floor(-3.55815));
System.out.println("Math.ceil(3.55815)=" + Math.ceil(3.55815));
System.out.println("Math.ceil(-3.55815)=" + Math.ceil(-3.55815));
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));
}
}
打印輸出:
Math.floor(3.55815)=3.0
Math.floor(-3.55815)=-4.0
Math.ceil(3.55815)=4.0
Math.ceil(-3.55815)=-3.0
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
- 座標圖利於理解:
總結技巧:
只需要明白一點,floor就是取地板,ceil取天花板,round的實現即時 +0.5然後計算floor