1 public class Test { 2 public static void main(String[] args) { 3 double d1 = 3.4, d2 = 3.6; //正数 4 double d3 = -3.4, d4 = -3.6; //负数 5 6 float f1 = 4.4F, f2 = 4.6F; //正数 7 float f3 = -4.4F, f4 = -4.6F; //负数 8 9 //floor()方法只能接收double类型,返回double类型10 //向下取整,返回小于参数的最大整数11 System.out.println(Math.floor(d1));//3.012 System.out.println(Math.floor(d2));//3.013 System.out.println(Math.floor(d3));//-4.014 System.out.println(Math.floor(d4));//-4.015 16 17 //ceil()方法只能接收double类型,返回double类型18 //向上取整,返回大于参数的最小整数19 System.out.println(Math.ceil(d1));//4.020 System.out.println(Math.ceil(d2));//4.021 System.out.println(Math.ceil(d3));//-3.022 System.out.println(Math.ceil(d4));//-3.023 24 25 //round()方法可以接收double类型,返回long类型26 //表示“四舍五入”,算法为Math.floor(x+0.5),即将参数加上0.5后再向下取整27 System.out.println(Math.round(d1));//328 System.out.println(Math.round(d2));//429 System.out.println(Math.round(d3));//-330 System.out.println(Math.round(d4));//-431 32 //round()方法可以接收float类型,返回int类型33 System.out.println(Math.round(f1));//434 System.out.println(Math.round(f2));//535 System.out.println(Math.round(f3));//-436 System.out.println(Math.round(f4));//-537 }38 }