您的位置:首页 > 其它

Math类的常用方法比较

2013-07-18 21:00 162 查看
ath类中提供了三个与取整有关的方法:ceil、floor、round,这些方法的作用与它们的英文名称的含义相对应,例如,ceil的英文意义是天花板,该方法就表示向上取整,Math.ceil(11.3)的结果为12,Math.ceil(-11.3)的结果是-11;floor的英文意义是地板,该方法就表示向下取整,Math.ceil(11.6)的结果为11,Math.ceil(-11.6)的结果是-12;最难掌握的是round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,加完之后刚好是整数的不用动,那个值即为结果,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。

System.out.println(Math.round(11.5)); //

System.out.println(Math.round(11.4)); //11
System.out.println(Math.round(-11.5)); //-11
System.out.println(Math.round(-11.4)); //-11
System.out.println(Math.round(-11.6));//-12
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: