Math:數學類 該類中包含了跟數學相關的公式
//1.圓周率
System.out.println(Math.PI);
//2.自然常數
System.out.println(Math.E);
//3.求絕對值
System.out.println(Math.abs(-10));
System.out.println(Math.abs(99));
//4.求最大值
System.out.println(Math.max(15, 26));
//5.求最小值
System.out.println(Math.min(15, 26));
與上述代碼相對應的結果
3.141592653589793
2.718281828459045
10
99
26
15
以下要注意的是Math.rint()中的參數小數位爲5時,偏向於取偶數
pow(a,b)指的是a的b次方
//6.求開方
System.out.println(Math.sqrt(4));
//7.求某數的任意次方
System.out.println(Math.pow(8, 2));
//8.取整系列
//向上取整:取比這個數大的最小整數
System.out.println(Math.ceil(8.9));
System.out.println(Math.ceil(8.1));
//向下取整:取比這個數小的最大整數
System.out.println(Math.floor(8.9));
System.out.println(Math.floor(8.1));
//四捨五入
System.out.println(Math.round(7.6));
System.out.println(Math.round(2.1));
//取離這個數最近的整數,如果是.5取的是偶數
System.out.println(Math.rint(8.9));
System.out.println(Math.rint(8.1));
System.out.println(Math.rint(8.5));
結果:
2.0
64.0
9.0
9.0
8.0
8.0
8
2
9.0
8.0
8.0