Java Round up to Upper value Math ceil()


java.lang.Math class comes with many methods to do simple basic numeric operations. One such one is ceil() method which returns always a nearest higher double value of a number.

One advantage of Math class methods is that they are declared as static so that they can be called without the need of object creation.

Following is the Math class signature as defined in java.lang package.

public final class Math extends Object

Following is the method signature as defined in Math class.

  • double ceil(double a): Returns the nearest upper double value of the double value passed as parameter.

for example:

1. The values 24.01 and 24.99 lies between 24.0 and 25.0. The ceil(24.01) and ceil(24.99) returns 25.0

2. The values -24.01 and -24.99 lies between -24.0 and -25.0 where -24.0 is higher. The ceil(-24.01) and ceil(-24.99) returns -24.0

Observe, the method parameter and return type are the same of double value.

Example on Java Round up to Upper value Math ceil() using the above method.
public class MathFunctionDemo
{
  public static void main(String args[])
  {
   System.out.println("Math.ceil(24.01) is " + Math.ceil(24.01));
   System.out.println("Math.ceil(24.99) is " + Math.ceil(24.99));

   System.out.println("\nMath.ceil(-24.01) is " + Math.ceil(-24.01));
   System.out.println("Math.ceil(-24.99) is " + Math.ceil(-24.99));
  }
}


Java Round up to Upper value Math ceil()
Output screenshot of Java Round up to Upper value Math ceil()

Leave a Comment

Your email address will not be published.