Common methods of JAVA Math

public class Test {

    public static void main(String[] args) {
        /**
         * Math.sqrt()//Calculate the square root
         * Math.cbrt()//Calculate the cube root
         * Math.pow(a,b)//Calculate the b power of a
         * Math.max( , )//Calculate the maximum value
         * Math.min( , )//Calculate the minimum value        
         */
        
        System.out.println(Math.sqrt(9));//3.0
        System.out.println(Math.cbrt(8));//2.0
        System.out.println(Math.pow(2, 3));//8.0
        System.out.println(Math.max(2.4, 3.8));//3.8
        System.out.println(Math.min(2.5, 8.4));//2.5
        
        /**
         * Math.abs()//Find the absolute value
         */
        
        System.out.println(Math.abs(-1.5));//1.5
        System.out.println(Math.abs(10.5));//10.5
        
        /**
         * Math.floor()//Round down, floor means floor;
         * Math.ceil()//Round up, ceil means ceiling;
         * Math.round()//Round to integer;
         * Math.rint()//It is also rounded to the nearest integer; but .5 will take an even number;
         */
        
        System.out.println(Math.floor(10));//10.0
        System.out.println(Math.floor(10.5));//10.0
        System.out.println(Math.floor(10.7));//10.0
        System.out.println(Math.ceil(11.2));//12.0
        System.out.println(Math.round(10.5));//11
        System.out.println(Math.rint(10.5));//10.0
        
        /**
         * Math.random()//Take a random number between 0 and 1 (including 0 but not 1);
         */
        
        System.out.println(Math.random());//0.0883798391075995
        System.out.println(Math.random() * 2);//0.35747170018174357
        System.out.println(Math.random() + 10);//10.405883407565078
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325897577&siteId=291194637