Math.floor():返回值是double類型的,返回的是不大於它的最大整數
舉例:
1 double x = Math.floor(5.8); 2 System.out.println(x); //輸出結果:5.0 3 double x = Math.floor(-2.5); 4 System.out.println(x); //輸出結果:-3.0
Math.ceil():返回值是double類型的,返回的是不小於它的最小整數
舉例:
1 double x = Math.ceil(5.8); 2 System.out.println(x); //輸出結果:6.0 3 double x = Math.ceil(-2.5); 4 System.out.println(x); //輸出結果:-2.0
Math.round():返回值是 int/long 類型的,返回的是四舍五入或四舍六入后的整數
(或者理解為Math.floor(x+0.5):在原來的數上+0.5再向下取整)
舉例:
1 int x = Math.round(1.6); 2 System.out.println(x); //輸出結果:2 3 int x = Math.round(1.3); 4 System.out.println(x); //輸出結果:1 5 6 int x = Math.round(-1.6); 7 System.out.println(x); //輸出結果:-2 8 int x = Math.round(-1.5); 9 System.out.println(x); //輸出結果:-1