Math.sqrt()//計算平方根
Math.cbrt()//計算立方根
Math.pow(a, b)//計算a的b次方
Math.max( , );//計算最大值
Math.min( , );//計算最小值
Math.abs求絕對值
Math.ceil天花板的意思,就是回傳大的值
Math.floor地板的意思,就是回傳小的值
Math.random 取得一個大于或者等于0.0小于不等于1.0的亂數
Math.rint 四舍五入,回傳double值
public class MathTest{ public static void main(String args[]){ /** *Math.sqrt()//計算平方根 *Math.cbrt()//計算立方根 *Math.pow(a, b)//計算a的b次方 *Math.max( , );//計算最大值 *Math.min( , );//計算最小值 */ System.out.println(Math.sqrt(16)); //4.0 System.out.println(Math.cbrt(8)); //2.0 System.out.println(Math.pow(3,2)); //9.0 System.out.println(Math.max(2.3,4.5));//4.5 System.out.println(Math.min(2.3,4.5));//2.3 /** * 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 System.out.println(Math.ceil(-1.7)); //-1.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 /** * 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)); //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 } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/153352.html
標籤:Java
