Languages
[Edit]
EN

Java - Math.ceil() method example

0 points
Created by:
Dirask Admin - Marwin
1430

The Math.ceil() function returns an integer value that is greater than or equal to the argument - the result of round-up operation. 

public class MathExample {

    public static void main(String[] args) {
        System.out.println( Math.ceil(  5     ) ); //  5.0

        System.out.println( Math.ceil(  2.49  ) ); //  3.0
        System.out.println( Math.ceil(  2.50  ) ); //  3.0
        System.out.println( Math.ceil(  2.51  ) ); //  3.0

        System.out.println( Math.ceil( -2.49  ) ); // -2.0
        System.out.println( Math.ceil( -2.50  ) ); // -2.0
        System.out.println( Math.ceil( -2.51  ) ); // -2.0

        System.out.println( Math.ceil(  0.999 ) ); //  1.0
        System.out.println( Math.ceil(  1.001 ) ); //  2.0
        System.out.println( Math.ceil( -1.001 ) ); // -1.0
    }
}

1. Documentation

Syntax
package java.lang;

public final class Math {

    public static double ceil(double number) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.ceil() call.

Parametersnumber - double value (primitive value).
Result

Rounded up number value (primitive value).

If input number value is equal to NaN it returns NaN.

If input  number value is equal to -Infinity it returns -Infinity.

If input  number value is equal to +Infinity it returns +Infinity.

Descriptionceil is a static method that takes only one parameter and returns a rounded-up value.

2. Rounding with precision up-to n places example

public class CustomMath {

    static double ceilPrecised(double number, int precision) {
        double power = Math.pow(10, precision);
        return Math.ceil(number * power) / power;
    }

    public static void main(String[] args) {
        System.out.println( ceilPrecised(     5  ,  0 ) ); // 5.0
        System.out.println( ceilPrecised(     5. ,  0 ) ); // 5.0
        System.out.println( ceilPrecised(      .5,  0 ) ); // 1.0

        System.out.println( ceilPrecised(  1.1234,  0 ) ); // 2.0
        System.out.println( ceilPrecised(  1.1234,  1 ) ); // 1.2
        System.out.println( ceilPrecised(  1.1235,  2 ) ); // 1.13
        System.out.println( ceilPrecised(  1.1235,  3 ) ); // 1.124

        System.out.println( ceilPrecised( -1.1234,  0 ) ); // -1.0
        System.out.println( ceilPrecised( -1.1234,  1 ) ); // -1.1
        System.out.println( ceilPrecised( -1.1234,  2 ) ); // -1.12
        System.out.println( ceilPrecised( -1.1234,  3 ) ); // -1.123

        System.out.println( ceilPrecised(    1234, -1 ) ); // 1240.0
        System.out.println( ceilPrecised(    1234, -2 ) ); // 1300.0
        System.out.println( ceilPrecised(    1234, -3 ) ); // 2000.0

        System.out.println( ceilPrecised(  5_000.000_001,  0 ) ); // 5001.0
        System.out.println( ceilPrecised(  5_000.000_001,  6 ) ); // 5000.000001
        System.out.println( ceilPrecised(  5_000.000_001, -3 ) ); // 6000.0
    }
}

References

  1. Floor and ceiling functions - Wikipedia

Java - Math object

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join