Home » Java programming language

Java StrictMath ceil() method with example

StrictMath Class ceil() method: Here, we are going to learn about the ceil() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 25, 2019

StrictMath Class ceil() method

  • ceil() method is available in java.lang package.
  • ceil() method is used to return the least or smallest value of the double type value which is greater than or equal to the given parameter.
  • ceil() method is a static method so this method is accessible with the class name too.
  • ceil() method does not throw any exception at the time of ceiling the given parameter.

Syntax:

    public static double ceil(double d);

Parameter(s):

  • double d – represents the double type value whose least value to be found.

Return value:

The return type of this method is double – it returns least value of the given parameter.

Note:

  • If we pass NaN as an argument, method returns the same value (NaN).
  • If we pass zero (0), method returns the same value with the same sign.
  • If we pass an infinity, method returns the same value with the same sign.
  • If we pass an argument which is less than 0, but greater than -1.0, method returns -0.0.
  • If we pass an argument whose value after the decimal point is greater than 0, method returns the value incremented by 1.

Example:

// Java program to demonstrate the example 
// of ceil(double d) method of StrictMath Class.

public class Ceil {
    public static void main(String[] args) {

        // Variable Declaration
        double d1 = -0.0;
        double d2 = 0.0;
        double d3 = -7.0 / 0.0;
        double d4 = 7.0 / 0.0;
        double d5 = -0.6;
        double d6 = 1000.0;
        double d7 = 1000.4;

        // Display previous value of d1,d2,d3,d4,d5 ,d6 and d7
        System.out.println("d1: " + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        System.out.println("d5: " + d5);
        System.out.println("d6: " + d6);
        System.out.println("d7: " + d7);

        // Here , we will get (-0.0) because we are passing 
        // parameter (-0.6)  because passed value is less than 0 
        // but greater than -1.0
        System.out.println("StrictMath.ceil(d1): " + StrictMath.ceil(d1));

        // Here , we will get (0.0) because we are passing parameter (0.0)
        System.out.println("StrictMath.ceil(d2): " + StrictMath.ceil(d2));

        // Here , we will get (-Infinity) because we are passing parameter (-7.0/0.0) 
        System.out.println("StrictMath.ceil(d3): " + StrictMath.ceil(d3));

        // Here , we will get (Infinity) because we are passing parameter (7.0/0.0) 
        System.out.println("StrictMath.ceil(d4): " + StrictMath.ceil(d4));

        // Here , we will get (-0.0) because we are passing 
        // parameter (-0.6) because passed value is less than 0 
        // but greater than -1.0
        System.out.println("StrictMath.ceil(d5): " + StrictMath.ceil(d5));

        // Here , we will get (1000.0) because we are passing 
        // parameter (1000.0) because passed value after decimal 
        // point is not greater than 0 so the same number is //returned
        System.out.println("StrictMath.ceil(d6): " + StrictMath.ceil(d6));

        // Here , we will get (1001.0) because we are passing 
        // parameter (1000.4)  because passed value after decimal 
        // point is greater than 0 so the number is incremented by 1 is returned
        System.out.println("StrictMath.ceil(d7): " + StrictMath.ceil(d7));
    }
}

Output

d1: -0.0
d2: 0.0
d3: -Infinity
d4: Infinity
d5: -0.6
d6: 1000.0
d7: 1000.4
StrictMath.ceil(d1): -0.0
StrictMath.ceil(d2): 0.0
StrictMath.ceil(d3): -Infinity
StrictMath.ceil(d4): Infinity
StrictMath.ceil(d5): -0.0
StrictMath.ceil(d6): 1000.0
StrictMath.ceil(d7): 1001.0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.