Math提供的三个取整方法

Math.ceil() 、Math.floor() 、Math.round() 方法 

   这些方法的作用与它们的英文含义相对应,例如ceil是天花板的意思,Math.ceil() 方法为向上取整的意思。Math.ceil(11.3)结果为12,Math.ceil(-11.3)结果为-11.

floor是地板的意思,Math.floor()为向下取整的意思。Math.floor(11.3)结果为11,Math.floor(-11.3)结果为-12.

最难掌握的就是Math.round()方法,它表示“四舍五入”,算法Math.round(11.3),即将原来的值加上0.5后再向下取整,

所以,Math.round(11.3)结果为11,Math.round(-11.3)结果为-10.

posted @ 2015-10-06 10:57  凌晨4点的洛杉矶  阅读(235)  评论(0编辑  收藏  举报