The difference between Math.round() Math.floor() Math.ceil() method in Java

The Math.rount() method is to round the incoming float or double data. The specific execution process is to first add the incoming parameter +0.5, and then round down.
Math.floor() method, round down. That is, the
Math.ceil() method returns the largest integer less than or equal to the parameter , rounding up. That is, it returns the smallest integer greater than or equal to the parameter.

public static void main(String[] args) {
    
    
        System.out.println("Math.round()方法  四舍五入");
        System.out.println("1.5  = "+Math.round(1.5));
        System.out.println("-1.5 = "+Math.round(-1.5));
        System.out.println("-1.6 = "+Math.round(-1.6));
        System.out.println("1.6  = "+Math.round(1.6));
        System.out.println("----------------");
        System.out.println("Math.floor()方法  向下取整");
        System.out.println("1.5  = "+Math.floor(1.5));
        System.out.println("-1.5 = "+Math.floor(-1.5));
        System.out.println("-1.6 = "+Math.floor(-1.6));
        System.out.println("1.6  = "+Math.floor(1.6));
        System.out.println("+----------------");
        System.out.println("Math.ceil()方法  向上取整");
        System.out.println("1.5  = "+Math.ceil(1.5));
        System.out.println("-1.5 = "+Math.ceil(-1.5));
        System.out.println("-1.6 = "+Math.ceil(-1.6));
        System.out.println("1.6  = "+Math.ceil(1.6));
    }

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_30033509/article/details/115048385