Commonly used methods in java.math package

Commonly used methods in java.math package

1.Math.floor(): Round down (but the result is a decimal):

public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.floor(11.5));//11.0
        System.out.println(Math.floor(11.6));//11.0
        System.out.println(Math.floor(11.4));//11.0
        System.out.println(Math.floor(-11.5));//-12.0
        System.out.println(Math.floor(-11.6));//-12.0
        System.out.println(Math.floor(-11.4));//-12.0
    }
}

2.Math.abs(): absolute value

public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.abs(11.5));//11.5
        System.out.println(Math.abs(11));//11
        System.out.println(Math.abs(-11.5));//11.5
        System.out.println(Math.abs(-11));//11
    }
}

3.Math.pow(a,b): power function, a to the b power;

public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.pow(2,3));//8.0
        System.out.println(Math.pow(2.0,3));//8.0
        System.out.println(Math.pow(2,3.0));//8.0
        System.out.println(Math.pow(2.0,3.0));//8.0
    }
}

4.Math.sqrt(): Arithmetic square root; (the result is double)

public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.sqrt(8));
        System.out.println(Math.sqrt(8.0));
        System.out.println(Math.sqrt(9));//3.0
        System.out.println(Math.sqrt(9.0));//3.0
    }
}

5.- Math.max(): maximum value; Math.min(): minimum value;

public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.max(2,5));//5
        System.out.println(Math.max(2.0,5));//5.0
        System.out.println(Math.max(2,5.0));//5.0
        System.out.println(Math.max(2.0,5.0));//5.0
        //min和max一样的道理
    }
}

6.Math.round() rounding

  • Law ; und(1.5) == 2; because round is rounded for positive numbers, for negative numbers: the law is who is close to, such as -11.5, -11.4 is -11 nearest to -11, and -11.6 is close to -12 , Which is -12;
public class TestDemo1 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(Math.round(1.5));//2
        System.out.println(Math.round(1.6));//2
        System.out.println(Math.round(1.4));//1
        System.out.println(Math.round(-1.5));//-1
        System.out.println(Math.round(-1.6));//-2
        System.out.println(Math.round(-1.7));//-2
    }
}

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/110425564