Get ceiling value of a number using Math.ceil in Java

Java 8Object Oriented ProgrammingProgramming

In order to get the ceiling value of a number in Java, we use the java.lang.Math.ceil() method. The Math.ceil() method returns the smallest (closest to negative infinity) double value which is greater than or equal to the parameter and has a value which is equal to a mathematical integer on the number line. If the parameter is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument. If the argument value is less than zero but greater than -1.0, then the value returned is negative zero.

Declaration − The java.lang.Math.ceil() method is declared as follows −

public static double ceil(double a)

Let us see a program to get the ceiling value of a number in Java.

Example

Live Demo

import java.lang.Math;
public class Example {
   public static void main(String[] args) {
      // declaring and initialising some double values
      double a = -100.01d;
      double b = 34.6;
      double c = 600;
      // printing their ceiling values
      System.out.println("Ceiling value of " + a + " = " + Math.ceil(a));
      System.out.println("Ceiling value of " + b + " = " + Math.ceil(b));
      System.out.println("Ceiling value of " + c + " = " + Math.ceil(c));
   }
}

Output

Ceiling value of -100.01 = -100.0
Ceiling value of 34.6 = 35.0
Ceiling value of 600.0 = 600.0
raja
Published on 04-Jan-2019 06:40:45
Advertisements