java中的Math.ceil、Math.floor和Math.round


ceil意為天花板,指向上取整;floor意為地板,指向下取整;round指四舍五入

package com.company;

public class Main {

    public static void main(String[] args) {
        //向上取整
        System.out.println(Math.ceil(11.3));//12.0
        System.out.println(Math.ceil(-11.3));//-11.0

        //向下取整
        System.out.println(Math.floor(11.3));//11.0
        System.out.println(Math.floor(-11.3));//-12.0

        //四舍五入 算法為Math.floor(x+0.5) 即原來的數字加上0.5再向下取整
        System.out.println(Math.round(11.4));//11
        System.out.println(Math.round(11.5));//12
        System.out.println(Math.round(11.6));//12
        System.out.println(Math.round(-11.4));//-11
        System.out.println(Math.round(-11.5));//-11
        System.out.println(Math.round(-11.6));//-12
    }
}

 


免責聲明!

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



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