Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - floor() method



The method floor gives the largest integer that is less than or equal to the argument.

double floor(double d) 
double floor(float f)

Parameters − A double or float primitive data type.

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

Example - Usage of floor method for double numbers

Following is an example of the usage of the method floor() 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.floor(a));
	  println(Math.floor(b));
	  println(Math.floor(c));
   } 
}

Output

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

1.0
1.0
1.0

Example - Usage of floor method for float numbers

Following is an example of the usage of the method floor() 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.floor(a));
	  println(Math.floor(b));
	  println(Math.floor(c));
   } 
}

Output

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

1.0
1.0
1.0

Example - Usage of floor method for negative double numbers

Following is an example of the usage of the method floor() 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.floor(a));
	  println(Math.floor(b));
	  println(Math.floor(c));
   } 
}

Output

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

-2.0
-2.0
-2.0
groovy_numbers.htm
Advertisements