更新時間:2019-09-07 09:00:00 來源:動力節(jié)點 瀏覽2812次
Java提供了Math工具類來完成復(fù)雜的運算,Math類是一個工具類,它的構(gòu)造器被private(私有)的,無法創(chuàng)建Math類的對象;Math類中基本上所有的類方法都是靜態(tài)方法,可以直接通過類名來調(diào)用它們。下面就隨動力節(jié)點java培訓(xùn)機構(gòu)小編一起看看Java math類的常用方法大全。
我們常用的方法有以下面這些:
Math.PI 記錄的圓周率
Math.E 記錄e的常量
Math.abs 求絕對值
Math.sin 正弦函數(shù) Math.asin 反正弦函數(shù)
Math.cos 余弦函數(shù) Math.acos 反余弦函數(shù)
Math.tan 正切函數(shù) Math.atan 反正切函數(shù) Math.atan2 商的反正切函數(shù)
Math.toDegrees 弧度轉(zhuǎn)化為角度 Math.toRadians 角度轉(zhuǎn)化為弧度
Math.ceil 得到不小于某數(shù)的最大整數(shù)
Math.floor 得到不大于某數(shù)的最大整數(shù)
Math.IEEEremainder 求余
Math.max 求兩數(shù)中最大
Math.min 求兩數(shù)中最小
Math.sqrt 求開方
Math.pow 求某數(shù)的任意次方, 拋出ArithmeticException處理溢出異常
Math.exp 求e的任意次方
Math.log10 以10為底的對數(shù)
Math.log 自然對數(shù)
Math.rint 求距離某數(shù)最近的整數(shù)(可能比某數(shù)大,也可能比它小)
Math.round 同上,返回int型或者long型(上一個函數(shù)返回double型)
Math.random 返回0,1之間的一個隨機數(shù)
下面看下常用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));
// 將弧度轉(zhuǎn)換角度
System.out.println("Math.toDegrees(3.14) = "+ Math.toDegrees(3.14));
// 將角度轉(zhuǎn)換為弧度
System.out.println("Math.toRadians(120) = "+ Math.toRadians(120));
// 取整,返回大于目標(biāo)數(shù)的最小整數(shù)。
System.out.println("Math.ceil(5.2) = " + Math.ceil(5.2));
// 取整,返回小于目標(biāo)數(shù)的最大整數(shù)。
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));
// 計算底數(shù)為 10 的對數(shù)。
System.out.println("Math.log10(5) = " + Math.log10(5));
// 計算自然對數(shù)
System.out.println("Math.log(10) = " + Math.log(10));
//求距離某數(shù)最近的整數(shù)
System.out.println("Math.rint(2.3) = " + Math.rint(2.3));
// 四舍五入取整
System.out.println("Math.round(5.5 ) = " + Math.round(5.5 ));
// 返回一個偽隨機數(shù),該值大于等于 0.0 且小于 1.0。
System.out.println("Math.random() = " + Math.random());
以上就是動力節(jié)點java培訓(xùn)機構(gòu)小編介紹的“Java math類的常用方法大全”內(nèi)容,希望對大家有幫助,如有任何疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。
相關(guān)免費視頻教程推薦
javase基礎(chǔ)視頻教程下載——數(shù)學(xué)相關(guān)的類:http://m.dabaquan.cn/xiazai/2519.html
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743