1. 程式人生 > >Java Math 類

Java Math 類

 

Math 數學類, 主要是提供了很多的數學公式。

/*
abs(double a)  獲取絕對值
ceil(double a)  向上取整
floor(double a)  向下取整
round(float a)   四捨五入
random()   產生一個隨機數. 大於等於 0.0 且小於 1.0 的偽隨機 double 值
*/

public class math_01 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("絕對值:"+Math.abs(-3));
        System.out.println("向上取整:"+Math.ceil(3.14));
        System.out.println("向下取整:"+Math.floor(-3.14)); //
        System.out.println("四捨五入:"+Math.round(3.54));
        System.out.println("隨機數:"+Math.random());
    }
}

執行結果:

絕對值:3
向上取整:4.0
向下取整:-4.0
四捨五入:4
隨機數