现在的位置: 首页 > 综合 > 正文

Java的Math类取整总结

2018年05月16日 ⁄ 综合 ⁄ 共 295字 ⁄ 字号 评论关闭

Java的Math类在面试题中时常出现,做个总结:

Math中有三个与取整有关的方法: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.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。

其中round的算法为Math.floor(x+0.5),意思是原来的数字加上0.5后再向下取整。

抱歉!评论已关闭.