【java笔记】Java中Math类的几个四舍五入方法的区别

下面来介绍将小数值舍入为整数的几个方法:Math.ceil()、Math.floor()和Math.round()。 这三个方法分别遵循下列舍入规则:
Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;

Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。

下面来看几个例子:

Math.ceil(25.9) //26

Math.ceil(25.5) //26

Math.ceil(25.1) //26

Math.ceil(25.0) //25

Math.round(25.9) //26

Math.round(25.5) //26

Math.round(25.1) //25

Math.floor(25.9) //25

Math.floor(25.5) //25

Math.floor(25.1) //25

因为强制取整时相当于Math.floor(),而Math.ceil()不常遇到,只需要记住四舍五入的Math.round()即可。 

转载:https://blog.csdn.net/flymoon1201/article/details/44776203

猜你喜欢

转载自blog.csdn.net/qq_42370146/article/details/82494827