Java——Math

一、介绍

Math包含执行基本数字运算的方法,如基本指数、对数、平方根和三角函数。所提供的都是静态方法,可以直接调用。

二、abs

public static int abs(int a)

abs方法用来获取参数a的绝对值

例子

public class MathTest {
    public static void main(String[] args) {
        int a = Math.abs(-6);
        int b = Math.abs(7);
        System.out.println(a);   // 6
        System.out.println(b);   // 7
    }
}

三、ceil

public static double ceil(double a)

ceil方法用来向上取整

例子

public class MathTest {
    public static void main(String[] args) {
        double a = Math.ceil(6.001);
        System.out.println(a);   // 7.0
    }
}

四、floor

public static double floor(double a)

floor方法用来向下取整

例子

public class MathTest {
    public static void main(String[] args) {
        double a = Math.floor(6.999);
        System.out.println(a);   // 6.0
    }
}

五、pow

public static double pow(double a, double b)

pow方法用来获取a的b次幂

例子

public class MathTest {
    public static void main(String[] args) {
        int a = 2, b = 3;
        System.out.println(Math.pow(a, b));   // 8.0
    }
}

六、round

public static long round(double a)

round方法用来四舍五入取整

例子

public class MathTest {
    public static void main(String[] args) {
        double a = Math.round(6.499);
        double b = Math.round(6.5);
        System.out.println(a);   // 6.0
        System.out.println(b);   // 7.0
    }
}
原文地址:https://www.cnblogs.com/xulinjun/p/14769707.html