Math.ceil()是常見編程語言中的常用代碼,用於向上取整數計算,返回的是大於或等於函數參數的數值。例如下面的幾個例子:
Math.ceil(12.3)//返回13.02.
Math.ceil(12.5)//返回13.0
Math.ceil(12.0)// 返回12.0
4.Math.ceil(10.000000001F)//返回10.0
:下面區分壹下floor,rint,round的區別的用法。
1.Math.floor(): 向下取整,即小於這個數的最大的那個整數。
Math.floor(2.2) = 2.0;?
Math.floor(-2.2) = -3.0;?
Math.floor(2.7) = 2.0;?
Math.floor(-2.7) = -3.0;
2.Math.rint() :返回最接近該值的那個整數。如果存在兩個這樣的整數,則返回其中的偶數。
3.Math.round() :四舍五入,註意返回的是整型。
例如代碼:Math.round(x) = Math.floor(x + 0.5)?
把以上代碼帶入以下實例測試得到的結果如下。
Math.round(2.2) = 2;?
Math.round(-2.2) = -2;?
Math.round(2.7) = 3;?
Math.round(-2.7) = -3?