java中Math常用方法

2021-09-14 01:28:22 字數 2570 閱讀 3573

/** 

*math.sqrt()//計算平方根

*math.cbrt()//計算立方根

*math.pow(a, b)//計算a的b次方

*math.max( , );//計算最大值

*math.min( , );//計算最小值

*/

system.out.println(math.sqrt(16));

system.out.println(math.cbrt(8));

system.out.println(math.pow(3,2));

system.out.println(math.max(2.3,4.5));

system.out.println(math.min(2.3,4.5));

/**

* abs求絕對值

*/

system.out.println(math.abs(-10.4));

system.out.println(math.abs(10.1));

/**

* ceil天花板的意思,就是返回大的值

*/

system.out.println(math.ceil(-10.1));

system.out.println(math.ceil(10.7));

system.out.println(math.ceil(-0.7));

system.out.println(math.ceil(0.0));

system.out.println(math.ceil(-0.0));

system.out.println(math.ceil(-1.7));

/**

* floor地板的意思,就是返回小的值

*/

system.out.println(math.floor(-10.1));

system.out.println(math.floor(10.7));

system.out.println(math.floor(-0.7));

system.out.println(math.floor(0.0));

system.out.println(math.floor(-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));

system.out.println(math.rint(10.7));

system.out.println(math.rint(11.5));

system.out.println(math.rint(10.5));

system.out.println(math.rint(10.51));

system.out.println(math.rint(-10.5));

system.out.println(math.rint(-11.5));

system.out.println(math.rint(-10.51));

system.out.println(math.rint(-10.6));

system.out.println(math.rint(-10.2));

/**

* 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

Java中Math類的常用方法

public class mathdemo 常用值與函式 math.pi 記錄的圓周率 math.e 記錄e的常量 math中還有一些類似的常量,都是一些工程數學常用量。math.abs 求絕對值 math.sin 正弦函式 math.asin 反正弦函式 math.cos 余弦函式 math.ac...

《java中Math類定義的常用方法》

math類定義的常用方法 math類定義的方法是靜態的,可以通過類名直接呼叫。下面簡要介紹幾類常用的方法。三角函式 public static double sin double a 三角函式正弦。public static double cos double a 三角函式余弦。public sta...

java常用的Math方法記錄

取 絕對值 math.abs 1 取 兩者最小值 math.min 1,2 取 兩者最大值 math.max 1,2 random 取得乙個 在 0.0,1.0 之間 的隨機數 math.random 不大於的最大整數 math.ceil 1.5 2.0 不大於的最大整數 math.floor 1....