java中Math常用方法


近期用到四舍五入想到以前整理了一點,就順便重新整理好經常見到的一些四舍五入,后續遇到常用也會直接在這篇博客更新。。。

public class Demo{
    public static void main(String args[]){  
        /** 
         *Math.sqrt()//計算平方根
         *Math.cbrt()//計算立方根
         *Math.pow(a, b)//計算a的b次方
         *Math.max( , );//計算最大值
         *Math.min( , );//計算最小值
         */  

        System.out.println(Math.sqrt(16));   //4.0 
        System.out.println(Math.cbrt(8));    //2.0
        System.out.println(Math.pow(3,2));     //9.0
        System.out.println(Math.max(2.3,4.5));//4.5
        System.out.println(Math.min(2.3,4.5));//2.3

        /** 
         * abs求絕對值 
         */  
        System.out.println(Math.abs(-10.4));    //10.4  
        System.out.println(Math.abs(10.1));     //10.1  

        /** 
         * ceil天花板的意思,就是返回大的值
         */  
        System.out.println(Math.ceil(-10.1));   //-10.0  
        System.out.println(Math.ceil(10.7));    //11.0  
        System.out.println(Math.ceil(-0.7));    //-0.0  
        System.out.println(Math.ceil(0.0));     //0.0  
        System.out.println(Math.ceil(-0.0));    //-0.0  
        System.out.println(Math.ceil(-1.7));    //-1.0

        /** 
         * floor地板的意思,就是返回小的值 
         */  
        System.out.println(Math.floor(-10.1));  //-11.0  
        System.out.println(Math.floor(10.7));   //10.0  
        System.out.println(Math.floor(-0.7));   //-1.0  
        System.out.println(Math.floor(0.0));    //0.0  
        System.out.println(Math.floor(-0.0));   //-0.0  

        /** 
         * random 取得一個大於或者等於0.0小於不等於1.0的隨機數 
         */  
        System.out.println(Math.random());  //小於1大於0的double類型的數
        System.out.println(Math.random()*2);//大於0小於1的double類型的數
        System.out.println(Math.random()*2+1);//大於1小於2的double類型的數

        /** 
         * rint 四舍五入,返回double值 
         * 注意.5的時候會取偶數    異常的尷尬=。=
         */  
        System.out.println(Math.rint(10.1));    //10.0  
        System.out.println(Math.rint(10.7));    //11.0  
        System.out.println(Math.rint(11.5));    //12.0  
        System.out.println(Math.rint(10.5));    //10.0  
        System.out.println(Math.rint(10.51));   //11.0  
        System.out.println(Math.rint(-10.5));   //-10.0  
        System.out.println(Math.rint(-11.5));   //-12.0  
        System.out.println(Math.rint(-10.51));  //-11.0  
        System.out.println(Math.rint(-10.6));   //-11.0  
        System.out.println(Math.rint(-10.2));   //-10.0  

        /** 
         * round 四舍五入,float時返回int值,double時返回long值 
         */  
        System.out.println(Math.round(10.1));   //10  
        System.out.println(Math.round(10.7));   //11  
        System.out.println(Math.round(10.5));   //11  
        System.out.println(Math.round(10.51));  //11  
        System.out.println(Math.round(-10.5));  //-10  
        System.out.println(Math.round(-10.51)); //-11  
        System.out.println(Math.round(-10.6));  //-11  
        System.out.println(Math.round(-10.2));  //-10  
    }  
}

 


免責聲明!

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



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