Math.ceil()、Math.floor()和Math.round()都是Math类中的静态方法,入参都是double(Double)类型,其中
ceil表示向上取整. floor表示向下取整, round表示四舍五入.
代码测试:
@Test
public void test(){
double[] tests = {0.16,1.22,2.65,3.25};
for (double test : tests) {
System.out.println("origin=" + test);
System.out.println("Math.round()= " + Math.round(test));
System.out.println("Math.ceil()= " + Math.ceil(test));
System.out.println("Math.floor()= " + Math.floor(test));
System.out.println("\n");
}
}
输出结果为:
在这里,我们可以简单的将Math.around()定义为,Math.around(a) = Math.floor(a + 0.5).