Math类中ceil、floor和round的用法

来源:互联网 发布:品浪渔具淘宝店促销 编辑:程序博客网 时间:2024/05/29 13:54

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。

0 0
原创粉丝点击