黑马程序员技术交流社区

标题: 关于Math.ceil(11.3),Math.floor(11.6),Math.round(11.5)三个方法的区别 [打印本页]

作者: jeasonlzy    时间: 2015-3-15 22:34
标题: 关于Math.ceil(11.3),Math.floor(11.6),Math.round(11.5)三个方法的区别
Math类中提供了三个与取整有关的方法:ceil、floor、round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,所以,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;floor的英文意义是地板,该方法就表示向下取整,所以,Math.floor(11.6)的结果为11,Math.floor(-11.6)的结果是-12;最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。


作者: jeasonlzy    时间: 2015-3-16 12:23
沙发自己坐。。
作者: 有丶时候    时间: 2015-3-16 13:02
赞一个!
作者: 虚谷    时间: 2015-3-16 22:47
赞一个。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2