更新時間:2019-09-07 09:00:00 來源:動力節點 瀏覽2835次
Java提供了Math工具類來完成復雜的運算,Math類是一個工具類,它的構造器被private(私有)的,無法創建Math類的對象;Math類中基本上所有的類方法都是靜態方法,可以直接通過類名來調用它們。下面就隨動力節點java培訓機構小編一起看看Java math類的常用方法大全。
我們常用的方法有以下面這些:
Math.PI 記錄的圓周率
Math.E 記錄e的常量
Math.abs 求絕對值
Math.sin 正弦函數 Math.asin 反正弦函數
Math.cos 余弦函數 Math.acos 反余弦函數
Math.tan 正切函數 Math.atan 反正切函數 Math.atan2 商的反正切函數
Math.toDegrees 弧度轉化為角度 Math.toRadians 角度轉化為弧度
Math.ceil 得到不小于某數的最大整數
Math.floor 得到不大于某數的最大整數
Math.IEEEremainder 求余
Math.max 求兩數中最大
Math.min 求兩數中最小
Math.sqrt 求開方
Math.pow 求某數的任意次方, 拋出ArithmeticException處理溢出異常
Math.exp 求e的任意次方
Math.log10 以10為底的對數
Math.log 自然對數
Math.rint 求距離某數最近的整數(可能比某數大,也可能比它小)
Math.round 同上,返回int型或者long型(上一個函數返回double型)
Math.random 返回0,1之間的一個隨機數
下面看下常用Math類代碼的寫法:
System.out.println("Math.PI = "+Math.PI);
System.out.println("Math.E = "+Math.E);
// 計算絕對值。
System.out.println("Math.abs(-8.5) = " + Math.abs(-8.5));
// 計算正弦
System.out.println("Math.sin(3.14) = " + Math.sin(3.14 ));
// 計算反正弦;。
System.out.println("Math.asin(0.5) = " + Math.asin(0.5));
// 計算三角余弦。
System.out.println("Math.cos(3.14) = " + Math.cos(3.14));
// 計算反余弦,
System.out.println("Math.acos(2.5) = " + Math.acos(2.5));
// 計算三角正切
System.out.println("Math.tan(0.2 ) = " + Math.tan(0.2 ));
// 計算反正切;。
System.out.println("Math.atan(1.5) = " + Math.atan(1.5));
// 計算商反正切;
System.out.println("Math.atan2(3.1,1) = " + Math.atan2(3.1, 1));
// 將弧度轉換角度
System.out.println("Math.toDegrees(3.14) = "+ Math.toDegrees(3.14));
// 將角度轉換為弧度
System.out.println("Math.toRadians(120) = "+ Math.toRadians(120));
// 取整,返回大于目標數的最小整數。
System.out.println("Math.ceil(5.2) = " + Math.ceil(5.2));
// 取整,返回小于目標數的最大整數。
System.out.println("Math.floor(-2.5 ) = " + Math.floor(-2.5));
// 求余
System.out.println("Math.IEEEremainder(10,4)= "+ Math.IEEEremainder(10,4));
// 找出最大值
System.out.println("Math.max(3.0 , 5.2) = " + Math.max(3.0 , 5.2));
// 計算最小值
System.out.println("Math.min(2.4 , 3.8) = " + Math.min(2.4 , 3.8));
// 求開方
System.out.println("Math.sqrt(1.5 ) = " + Math.sqrt(1.5 ));
// 計算乘方
System.out.println("Math.pow(9, 2) = " + Math.pow(9, 2));
// 求e的任意次方
System.out.println("Math.exp(5) = " + Math.exp(5));
// 計算底數為 10 的對數。
System.out.println("Math.log10(5) = " + Math.log10(5));
// 計算自然對數
System.out.println("Math.log(10) = " + Math.log(10));
//求距離某數最近的整數
System.out.println("Math.rint(2.3) = " + Math.rint(2.3));
// 四舍五入取整
System.out.println("Math.round(5.5 ) = " + Math.round(5.5 ));
// 返回一個偽隨機數,該值大于等于 0.0 且小于 1.0。
System.out.println("Math.random() = " + Math.random());
以上就是動力節點java培訓機構小編介紹的“Java math類的常用方法大全”內容,希望對大家有幫助,如有任何疑問,請在線咨詢,有專業老師隨時為你服務。
相關免費視頻教程推薦
javase基礎視頻教程下載——數學相關的類:http://m.ilovecolors.com.cn/xiazai/2519.html
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習