Bootstrap

Java:Math类

一、概念

Math 类是一个帮助我们用于数学计算的工具类,其中的方法都是静态的可以直接通过类名调用方法,构造方法是私有化无法实例化。


二、常用方法

public class Test {
    public static void main(String[] args) {
        // abs:获取参数绝对值
        System.out.println("====== abs ======");
        System.out.println(Math.abs(-10));
        // ceil:向上取整数
        System.out.println("====== ceil ======");
        System.out.println(Math.ceil(1.5));
        // floor:向下取整数
        System.out.println("====== floor ======");
        System.out.println(Math.floor(1.5));
        // round:四舍五入
        System.out.println("====== round ======");
        System.out.println(Math.round(1.4));
        System.out.println(Math.round(1.5));
        // max:获取两个值较大的那个
        System.out.println("====== max ======");
        System.out.println(Math.max(2, 3));
        // min:获取两个值较小的那个
        System.out.println("====== min ======");
        System.out.println(Math.min(2, 3));
        // pow:返回a的b次方的值
        System.out.println("====== pow ======");
        System.out.println(Math.pow(2, 3));
        // random:返回 [0.0 - 1。0)之间的值,可以使用上述的方法取整数
        System.out.println("====== random ======");
        System.out.println(Math.random());
    }
}

运行结果:

====== abs ======
10
====== ceil ======
2.0
====== floor ======
1.0
====== round ======
1
2
====== max ======
3
====== min ======
2
====== pow ======
8.0
====== random ======
0.14959849166979422

;