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。

 

若有错误之处,欢迎指正。谢谢!

posted @ 2018-04-15 16:57  hong_Yu  阅读(24672)  评论(0编辑  收藏  举报