java tutorial - Java Math.ceil() Method - java programming - learn java - java basics - java for beginners



Java numbers ceil

Learn Java - Java tutorial - Java numbers ceil - Java examples - Java programs

Description

The Math.ceil () method gives an integer with a zero fractional part closest to the number of the argument to the right, in other words rounding the fraction to the larger side.

Syntax

The method has the following options:

double ceil(double d)
double ceil(float f)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • Primitive data type is double or float.

Return value

  • In Java, Math.ceil () returns an integer that is closest to the number of the argument to the right. Returns as a double.

Sample Code

public class Test { 

   public static void main(String args[]) {
      double d1 = 0.3;
      double d2 = -10.75;
      double d3 = 8.4;
      double d4 = 11;
      double d5 = -11;
      
      float f1 = -0.3f;
      float f2 = -5.75f;
      float f3 = 3.4f;
      float f4 = 8;
      float f5 = -8;

      System.out.println("d1 = " + Math.ceil(d1));
      System.out.println("d2 = " + Math.ceil(d2));
      System.out.println("d3 = " + Math.ceil(d3)); 
      System.out.println("d4 = " + Math.ceil(d4)); 
      System.out.println("d5 = " + Math.ceil(d5)); 
      System.out.println("--------------");  
      System.out.println("f1 = " + Math.ceil(f1)); 
      System.out.println("f2 = " + Math.ceil(f2));
      System.out.println("f3 = " + Math.ceil(f3));
      System.out.println("f4 = " + Math.ceil(f4)); 
      System.out.println("f5 = " + Math.ceil(f5));
   }
}
click below button to copy the code. By - java tutorial - team

Output

d1 = 1.0
d2 = -10.0
d3 = 9.0
d4 = 11.0
d5 = -11.0
--------------
f1 = -0.0
f2 = -5.0
f3 = 4.0
f4 = 8.0
f5 = -8.0

Related Searches to Java Math.ceil() Method