×

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 Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.