1. 程式人生 > >Math.ceil()、Math.floor()和Math.round()

Math.ceil()、Math.floor()和Math.round()

分享 .com 方法 com ID inf 整數 分享圖片 img

下面來介紹將小數值舍入為整數的幾個方法:Math.ceil()、Math.floor()和Math.round()。


這三個方法分別遵循下列舍入規則:
Math.ceil()執行向上舍入,即它總是將數值向上舍入為最接近的整數;
Math.floor()執行向下舍入,即它總是將數值向下舍入為最接近的整數;
Math.round()執行標準舍入,即它總是將數值四舍五入為最接近的整數(這也是我們在數學課上學到的舍入規則)。

技術分享圖片

Math.ceil()、Math.floor()和Math.round()