Math類的常用方法

2022-03-12 06:18:07 字數 850 閱讀 7995

system.out.println("返回引數的絕對值:" + math.abs(-9)); //9

//ceil天花板的意思,就是返回大的值,注意一些特殊值

system.out.println("對整形變數向上取整,返回型別為double型:" + math.ceil(-8.7));

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

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

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

system.out.println("對整型變數向下取整。返回型別為double型別:" + math.floor(-8.1));

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

//rint 四捨五入,返回double值 (注意.5的時候會取偶數 )

system.out.println("返回與引數最接近的整數。返回型別為double: " + math.rint(4.5));

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

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

//round 四捨五入,float時返回int值,double時返回long值

system.out.println("返回乙個最接近的int、long型值:" + math.round(5.5)); //6

// random 取得乙個大於或者等於0.0小於不等於1.0的隨機數

system.out.println("返回乙個隨機數:"+math.random());

Math類常用方法

名稱說明 abs已過載。返回指定數字的絕對值。acos 返回余弦值為指定數字的角度。asin 返回正弦值為指定數字的角度。atan 返回正切值為指定數字的角度。atan2 返回正切值為兩個指定數字的商的角度。bigmul 生成兩個 32 位數字的完整乘積。ceiling 已過載。返回大於或等於指定數...

Math類的常用方法

封裝了一些基本運算方法,包括進行三角運算的正弦 余弦 正切 餘切相關的方法 例如,求正弦的sin,求余弦的cos等,如果使用的話可以參考jdk。下面的方法可能是我們經常要使用的 1 求最大值,可以用於求int型別,long型別,float型別,double型別的最大值,下面僅僅下求整數最大值的方法的...

Math類的常用方法

細節決定成敗。寫程式如此,生活亦是如此。math類包含執行基本數字運算的方法,如基本指數,對數,平方根和三角函式。主要驗證一下math類裡面的常用方法 1 round double a 返回引數中最接近的 long 其中 long四捨五入為正無窮大。system.out.println math.r...