Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - round() method



The method round() returns the closest long or int, as given by the methods return type.

Syntax

long round(double d)
  
int round(float f) 

Parameters

  • d − A double or float primitive data type.

  • f − A float primitive data type.

Return Value

This method returns the closest long or int, as indicated by the method's return type, to the argument.

Example - Usage of round method for positive double values

Following is an example of the usage of this method −

Example.groovy

class Example { 
   static void main(String[] args){ 
      double d = 100.675; 
      double e = 100.500; 
		
      println(Math.round(d)); 
      println(Math.round(e)); 
   } 
}

Output

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

101
101 

Example - Usage of round method for positive float values

Following is an example of the usage of this method −

Example.groovy

class Example { 
   static void main(String[] args){ 
      float d = 100.675f; 
      float e = 100.500f; 
		
      println(Math.round(d)); 
      println(Math.round(e)); 
   } 
}

Output

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

101
101 

Example - Usage of round method for negative double values

Following is an example of the usage of this method −

Example.groovy

class Example { 
   static void main(String[] args){ 
      double d = -100.675; 
      double e = -100.500; 
		
      println(Math.round(d)); 
      println(Math.round(e)); 
   } 
}

Output

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

-101
100 
groovy_numbers.htm
Advertisements