重刷一遍面试题,记录学习过程中遇到的问题。
1. java中进行取整函数的总结:
java中常用的取整函数就是Math类下的ceil、floor、round三种取整方式。
ceil:向上取整,要点就是越取越大,比如:
Math.ceil(11.1) = 12.0
Math.ceil(12.7) = 13.0
Math.ceil(-11.5) = -11.0
Math.ceil(-7.7) = -7.0
floor:向下取整,要点就是越取越小,比如:
Math.floor(11.1) = 11.0
Math.floor(12.7) = 12.0
Math.floor(-11.5) = -12.0
Math.floor(-7.7) = -8.0
round:四舍五入,要点就是去掉符号,然后将数字四舍五入后再加上符号,但是有例外:当数字为负数,且小数位最后一位为5时,直接去掉最后一位小数位,剩余部分继续按照前面的规则计算即可,比如:
Math.round(11.1) = 11.0
Math.round(12.7) = 13.0
Math.round(-11.5) = -11.0
Math.round(-7.7) = -8.0
Math.round(-11.75) -> Math.round(-11.7) -> -12 .0