Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - ceil() method



The method ceil gives the smallest integer that is greater than or equal to the argument.

double ceil(double d) 
double ceil(float f)

Parameters − A double or float primitive data type.

Return Value − This method returns the smallest integer that is greater than or equal to the argument. Returned as a double.

Example - Usage of ceil method for double numbers

Following is an example of the usage of the method ceil() for double numbers.

Example.groovy

class Example { 
   static void main(String[] args) {  
      double a = 1.4;
	  double b = 1.5;
	  double c = 1.6;
		
      println(Math.ceil(a));
	  println(Math.ceil(b));
	  println(Math.ceil(c));
   } 
}

Output

When we run the above program, we will get the following result −

2.0
2.0
2.0

Example - Usage of ceil method for float numbers

Following is an example of the usage of the method ceil() for float numbers.

Example.groovy

class Example { 
   static void main(String[] args) {  
      float a = 1.4f;
	  float b = 1.5f;
	  float c = 1.6f;
		
      println(Math.ceil(a));
	  println(Math.ceil(b));
	  println(Math.ceil(c));
   } 
}

Output

When we run the above program, we will get the following result −

2.0
2.0
2.0

Example - Usage of ceil method for negative double numbers

Following is an example of the usage of the method ceil() for negative double numbers.

Example.groovy

class Example { 
   static void main(String[] args) {  
      double a = -1.4;
	  double b = -1.5;
	  double c = -1.6;
		
      println(Math.ceil(a));
	  println(Math.ceil(b));
	  println(Math.ceil(c));
   } 
}

Output

When we run the above program, we will get the following result −

-1.0
-1.0
-1.0
groovy_numbers.htm
Advertisements