×

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument.

Syntax:

public static double ceil(double a)

Parameters:

The parameter ‘a’ represents a value.

Return Value:

The ceil() method returns the smallest floating-point value greater than or equal to argument and is equal to a mathematical integer.

  • It returns same value as the argument, if the argument value is equal to a mathematical integer.
  • It returns the same value as the argument, if we pass a NaN or infinity or positive zero or negative zero argument.
  • It will return negative zero, if the argument value is less than zero but greater than -1.0.

Example 1:

public class JavaMathCeilExample1 {
    public static void main(String[] args) {
        double a=8.1;
        //returns the smallest value which is greater than or equal to argument
        System.out.println("Smallest closest double value of "+ a +" =
"+Math.ceil(a));
    }
}

Output:

Smallest closest double value of 8.1 = 9.0

Example 2:

public class JavaMathCeilExample2 {
    public static void main(String[] args) {
        double a=-0.0d/0.0d;
        //return NaN value
        System.out.println("Smallest closest double value of "+ a +" =
"+Math.ceil(a));
    }
}

Output:

Smallest closest double value of NaN = NaN

Example 3:

public class JavaMathCeilExample3 {
    public static void main(String[] args) {
        double a=-0.0;
        //returns a zero with the same sign as the argument ,if the argument is
zero.
        System.out.println("Smallest closest double value of "+ a +" =
"+Math.ceil(a));
    }
}

Output:

Smallest closest double value of -0.0 = -0.0

Example 4:

public class JavaMathCeilExample4 {
    public static void main(String[] args) {
        double a=-5/0.0;
        //returns infinity with the same sign as the argument ,if the argument
is infinity.
        System.out.println("Smallest closest double value of "+ a +" = "
+Math.ceil(a));
    }
}

Output:

Smallest closest double value of -Infinity = -Infinity

Related Topics

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Difference Between Data Hiding and Abstraction in Java

Abstraction: Data Abstraction and Data Hiding ideas are utilised to show the expected data to the end client and conceal the superfluous subtleties, however, for specific purposes like decreasing the framework's...

10 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.