Math类中的取整方法

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。

原文地址:https://www.cnblogs.com/CuiHongYu/p/9987815.html