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后再向下取整。