Math类

    技术2022-07-10  137

    Math类

    以下介绍一些Math类中常用的方法,由于Math类为final且构造方法为private,所以在使用Math类时直接用类名调用方法。 1.返回参数的绝对值 Math.abs(); 2.返回大于或等于参数的最小整数。返回值为double类型 Math.ceil();//向大取整 3.返回小于或等于参数的最大整数。返回值为double类型 Math.floor();向小取整 4.返回值与参数最接近的整数,如果参数与两个整数的距离相等,则返回其中为偶数的那一个。返回值为double类型 Math.rint(); 5.根据方法返回值类型返回与参数最接近的long或int类型的值 Math.round(); 6.返回两个参数中最小值 Math.min(); 7.返回两个参数中最大值 Math.max();

    package SE01.n5Math; public class demo01Math { public static void main(String[] args) { // Math类不能new对象,其中的方法使用类名点来调用 // 1.返回参数的绝对值 System.out.println(Math.abs(-123)); // 2.返回大于或等于参数的最小整数。返回值作为double类型 System.out.println(Math.ceil(3.14));//4.0 System.out.println(Math.ceil(-3.14));//-3.0 // 3.返回小于或等于参数的最大整数。返回值作为double类型 System.out.println(Math.floor(3.14));//3 System.out.println(Math.floor(-3.14));//-4 // 4.返回值与参数最接近的整数,返回值作为double类型 System.out.println(Math.rint(3.14));//3.0 四舍五入 System.out.println(Math.rint(-3.14));//-3.0 四舍五入 // 5.根据方法返回值类型返回与参数最接近的long或int类型的值 long lo=Math.round(3.5); System.out.println(lo);//4 ->对小数位进行四舍五入 int in=Math.round(3.14f);//3 ->四舍五入 System.out.println(in); // 6.返回两个参数中的最小值 System.out.println(Math.min(3.45, 21)); // 7.返回两个参数中的最大值 System.out.println(Math.max(3.22, 32)); } }
    Processed: 0.009, SQL: 9