java基礎中Math.round用法

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

發佈了24 篇原創文章 · 獲贊 12 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章