Math的三个取整方法。

 Math类中提供了三个与取整有关的方法:ceilfloorround,这些方法的作用与它们的英文名称的含义相对应

1.ceil的英文意义是天花板,该方法就表示向上取整,所以,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11

2.floor的英文意义是地板,该方法就表示向下取整,所以,Math.floor(11.6)的结果为11,Math.floor(-11.6)的结果是-12

3.round方法,它表示四舍五入,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12Math.round(-11.5)的结果为-11

 

记住英文含义即可。

posted @ 2016-04-08 11:20  zerocoin  阅读(425)  评论(0编辑  收藏  举报