1. 程式人生 > >Math類中的取整方法

Math類中的取整方法

round 與他 向上取整 mat 即將 數字 英文名字 1.5 就是

Math類提供了3個有關取整的方法:ceil()、floor()、round()。

這些方法與他們的英文名字相對應:

  ceil,天花板,意思就是向上取整,Math.ceil(11.5)的結果為12,Math.ceil(-11.5)的結果為-11。

  floor,地板,意思就是向下取整,Math.floor(11.5)的結果為11,Math.floor(-11.5)的結果為-12。

  round,表示四舍五入,算法為:Math.floor(x+0.5),

      即將原來的數字加上0.5後在向下取整,Math.round(11.5)的結果為12,Math.round(-11.5)的結果為-11。

Math類中的取整方法