实例
先看一个正数例子
System.out.println(Math.ceil(11.1f)); //12.0
System.out.println(Math.ceil(11.6f)); //12.0
System.out.println(Math.round(11.1f)); //11
System.out.println(Math.round(11.6f)); //12
System.out.println(Math.floor(11.1f)); //11.0
System.out.println(Math.floor(11.6f)); //11.0
负数例子
System.out.println(Math.ceil(-11.1f)); //-11.0
System.out.println(Math.ceil(-11.6f)); //-11.0
System.out.println(Math.round(-11.1f)); //-11
System.out.println(Math.round(-11.6f)); //-12
System.out.println(Math.floor(-11.1f)); //-12.0
System.out.println(Math.floor(-11.6f)); //-12.0
- Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
- Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;
- Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。
英文单词
ceil的含义
round的含义
floor的含义
结论
round是四舍五入。ceil是向上取整。floor向下取整。