Math类中取整方法的使用


Math.round() 与 Math.ceil()  与 Math.floor()区别


public class Demo {
    public static void main(String[] args) {
    	double a1 = Math.ceil(11.1); //向上取整
    	double b1 = Math.ceil(11.5);
    	double c1 = Math.ceil(-11.1);
    	
    	double a2 = Math.floor(11.1);//向下取整
    	double b2 = Math.floor(11.5);
    	double c2 = Math.floor(-11.1);
    	
    	double a3 = Math.round(11.1);//四舍五入
    	double b3 = Math.round(11.5);
    	double c3 = Math.round(-11.1);
    	
    	System.out.println("a1:" + a1 + "  b1:" + b1 + "  c1:" + c1
    			 + "\na2:" + a2  + "  b2:" + b2 + "  c2:" + c2
    			 + "\na3:" + a3  + "  b3:" + b3 + "  c3:" + c3);
    }
}

运行结果

a1:12.0  b1:12.0  c1:-11.0
a2:11.0  b2:11.0  c2:-12.0
a3:11.0  b3:12.0  c3:-11.0


你可能感兴趣的:(【Java基础】)