java基礎之二:取整函數(Math類)


在日常開發中經常會遇到數字的情況,有關數據的場景中會遇到取整的情況,java中提供了取整函數。看下java.lang.Math類中取整函數的用法。

一、概述

java.lang.Math類中有三個和取整相關的函數,分別是ceil()、floor()、round()方法。所謂取整就是舍棄小數位,保留整數位,但是如何舍棄和保留每個方法的處理方式不一樣,看其具體用法。

二、詳述

ceil()方法

ceil方法的作用是向上取整。下面看方法的定義,

接收一個double類型的參數,返回double類型。

正數情況

下面看參數為正數的情況,ceil是如何取整的,

package com.example.demo.test;

public class TestMathCeilPost {
    public static void main(String[] args) {

        //定義double類型
        double b=12.5;
        double b2=12.1;

        //向上取整
        double d=Math.ceil(b);
        double d2=Math.ceil(b2);
        //轉化為int類型
        int a=Double.valueOf(d).intValue();
        int a2=Double.valueOf(d2).intValue();

        System.out.println(b+"調用Math.ceil方法后的值為:"+a);
        System.out.println(b2+"調用Math.ceil方法后的值為:"+a2);




    }
}

看執行結果,

通過上面的結果,可以看到在正數情況下是向上取整,也就是大於原始結果的最小的正數,

負數情況

看負數的情況,

package com.example.demo.test;

public class TestMathCeilNegative {
    public static void main(String[] args) {

        //定義double類型
        double b=-12.5;
        double b2=-12.1;

        //向上取整
        double d=Math.ceil(b);
        double d2=Math.ceil(b2);
        //轉化為int類型
        int a=Double.valueOf(d).intValue();
        int a2=Double.valueOf(d2).intValue();

        System.out.println(b+"調用Math.ceil方法后的值為:"+a);
        System.out.println(b2+"調用Math.ceil方法后的值為:"+a2);


    }
}

看執行結果,

可以看出也是取大於給定數的最小的負整數。

floor()

floor方法的作用是向下取整,看方法定義如下,

正數情況

看正數情況下,

package com.example.demo.test;

public class TestMathFloorPost {
    public static void main(String[] args) {

        //定義double類型
        double b=12.5;
        double b2=12.1;

        //向下取整
        double d=Math.floor(b);
        double d2=Math.floor(b2);
        //轉化為int類型
        int a=Double.valueOf(d).intValue();
        int a2=Double.valueOf(d2).intValue();

        System.out.println(b+"調用Math.floor方法后的值為:"+a);
        System.out.println(b2+"調用Math.floor方法后的值為:"+a2);
    }
}

看執行結果,

通過上面的結果,可以看到floor方法在正數情況下,是取小於給定數的最大的正數。

負數情況

看負數情況下,

package com.example.demo.test;

public class TestMathFloorNegative {
    public static void main(String[] args) {

        //定義double類型
        double b=-12.5;
        double b2=-12.1;

        //向下取整
        double d=Math.floor(b);
        double d2=Math.floor(b2);
        //轉化為int類型
        int a=Double.valueOf(d).intValue();
        int a2=Double.valueOf(d2).intValue();

        System.out.println(b+"調用Math.floor方法后的值為:"+a);
        System.out.println(b2+"調用Math.floor方法后的值為:"+a2);
    }
}

看執行結果,

通過上面的結果,可以看到floor方法在負數情況下,是取小於給定數的最大的正數。

round()

round方法的作用是“四舍五入”,看方法定義,

看到方法的入參有float、double,出參對應這int、long。以double為例。

正數情況

看正數的情況,

package com.example.demo.test;

public class TestMathRoundPost {
    public static void main(String[] args) {

        //定義double類型
        double b=12.5;
        double b2=12.1;

        //向上取整
        double d=Math.round(b);
        double d2=Math.round(b2);
        //轉化為int類型
        int a=Double.valueOf(d).intValue();
        int a2=Double.valueOf(d2).intValue();

        System.out.println(b+"調用Math.round方法后的值為:"+a);
        System.out.println(b2+"調用Math.round方法后的值為:"+a2);
    }
}

看執行結果,

看執行結果就是進行數學上的四舍五入運算,得到結果。

負數情況

看負數情況,

package com.example.demo.test;

public class TestMathRoundNegative {
    public static void main(String[] args) {

        //定義double類型
        double b=-12.6;
        double b2=-12.1;
        double b3=-12.5;


        double d=Math.round(b);
        double d2=Math.round(b2);
        double d3=Math.round(b3);
        //轉化為int類型
        int a=Double.valueOf(d).intValue();
        int a2=Double.valueOf(d2).intValue();
        int a3=Double.valueOf(d3).intValue();

        System.out.println(b+"調用Math.round方法后的值為:"+a);
        System.out.println(b2+"調用Math.round方法后的值為:"+a2);
        System.out.println(b3+"調用Math.round方法后的值為:"+a3);
    }
}

看執行結果,

從上面的結果可以看到,在負數情況下不是簡單的“四舍五入”,而要考慮XXX.5的情況,小數位上的數字小於等於5,舍棄小數位,保留整數位數;小數位上的數字大於5,舍棄小數位且加-1;

三、總結

本文分析了Math類中的ceil、floor、round函數,

ceil

正數和負數情況下都是向上取整,這里的向上指的是取大於給定數字的最小整數,例,ceil(12.3)-->13.0 ceil(-12.6)-->-12.0

floor

正數和負數情況下都是向下取整,這里的向下指的是取小於給定數字的最大整數,例,floor(12.3)-->12.0 floor(-12.6)-->-13.0

round

正數情況下是數學上的四舍五入,例,round(12.3)-->12 round(12.5)-->13

正數情況下需要注意小數位的數字,小於等於5,則舍去小數位;大於5,則加-1,例,round(-12.3)-->-12 round(-12.5)-->-12 round(-12.6)-->-13

 

有不正之處,歡迎指正,感謝!


免責聲明!

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



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