Java中Math類的常用方法

public class MathDemo {  
    public static void main(String args[]){  
        /** 
         * 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  
          
        /** 
         * 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  
          
        /** 
         * max 兩個中返回大的值,min和它相反,就不舉例了 
         */  
        System.out.println(Math.max(-10.1, -10));   //-10.0  
        System.out.println(Math.max(10.7, 10));     //10.7  
        System.out.println(Math.max(0.0, -0.0));    //0.0  
          
        /** 
         * random 取得一個大於或者等於0.0小於不等於1.0的隨機數 
         */  
        System.out.println(Math.random());  //0.08417657924317234  
        System.out.println(Math.random());  //0.43527904004403717  
          
        /** 
         * 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  
    }  
}  

轉自:http://blog.csdn.net/lixiang0522/article/details/7557851

Math.PI 記錄的圓周率,其值爲:3.14159265358979323846

Math.E記錄e的常量,其值爲:2.7182818284590452354

  8)Math.sqrt(25);//返回指定數字的平方根(取到的數字的值,這個函數用來給參數開方)返回5

  9)Math.IEEEremainder(f1, f2):求餘
      Math.pow(x,y):x的y次方  (求某數的任意次方, 拋出ArithmeticException處理溢出異常
      Math.sqrt(x):平方根
      Math.exp(x) 求e的任意次方
      Math.log10(x) 以10爲底的對數
      Math.log(x)自然對數
  10)三角函數中的方法:
      Math.sin() 正弦函數 Math.asin() 反正弦函數
      Math.cos() 餘弦函數 Math.acos() 反餘弦函數
      Math.tan() 正切函數 Math.atan() 反正切函數 Math.tan() 返回正切值爲指定數字的角度)
      Math.atan2() 商的反正切函數
      Math.toDegrees() 弧度轉化爲角度 Math.toRadians() 角度轉化爲弧度


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