Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - abs() method



The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte.

Syntax

double abs(double d) 
float abs(float f) 
int abs(int i) 
long abs(long lng)

Parameters − Any primitive data type.

Return Value − This method Returns the absolute value of the argument.

Example - Getting absolute value of double values

Following is an example of the usage of this method for double values.

Example.groovy

class Example { 
   static void main(String[] args) { 
      double a = -8.0; 
      double b = 100.0;  
		
      println(Math.abs(a)); 
      println(Math.abs(b)); 
   } 
}

Output

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

8.0 
100.0

Example - Getting absolute value of integer values

Following is an example of the usage of this method for integer values.

Example.groovy

class Example { 
   static void main(String[] args) { 
      int a = -8; 
      int b = 100;  
		
      println(Math.abs(a)); 
      println(Math.abs(b)); 
   } 
}

Output

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

8
100

Example - Getting absolute value of float values

Following is an example of the usage of this method for float values.

Example.groovy

class Example { 
   static void main(String[] args) { 
      float a = -8.0f; 
      float b = 100f;  
		
      println(Math.abs(a)); 
      println(Math.abs(b)); 
   } 
}

Output

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

8.0 
100.0
groovy_numbers.htm
Advertisements