Math
Math有很多數學方法,以下介紹常用。
abs絕對值
1
System.out.println(Math.abs(-9));
9
pow次方
2的4次方。
1
System.out.println(Math.pow(2, 4));
16
ceil
傳回大於或等於參數的「整數」,不是小數。
以下程式碼傳回 >= 3.99的「整數」,ceil回傳值是double。
1
System.out.println(Math.ceil(3.99));
4.0
floor
傳回小於或等於參數的「整數」,不是小數。
以下程式碼傳回 <= 3.99的「整數」,floor回傳值是double。
1
System.out.println(Math.floor(3.99));
3.0
round四捨五入
1
2
System.out.println(Math.round(3.99));
System.out.println(Math.round(3.45));
4
3
強制轉型成int是無條件捨去小數點。
1
2
System.out.println((int) 3.99);
System.out.println((int) 3.45);
3
3
sqrt開根號
3 * 3 = 9,9的開根號是3。
1
System.out.println(Math.sqrt(9));
3
min 二個數字取最小值
二個數字可以放int, float, double
1
System.out.println(Math.min(3.5, 5.6));
3.5
max 二個數字取最大值
1
System.out.println(Math.max(3.5, 5.6));
5.6