Groovy Math.abs - Syntax
mixed abs(mixed n);
The Math.abs() function in Groovy is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
// Positive integer value
def n = Math.abs(9);
println("The absolute value of 9 is " + n);
// Negative integer value
def x = Math.abs(-6);
println("The absolute value of -6 is " + x);
}
}
Convert the positive and negative float to an absolute value.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
// Positive floating point value
def n = Math.abs(9.3);
println("The absolute value of 9.3 is " + n);
// Negative floating point value
def x = Math.abs(-6.3);
println("The absolute value of -6.3 is " + x);
}
}
Convert the positive and negative Infinity to an absolute value.
import java.lang.Math;
class MathExample {
public static void main(String[] args) {
// Positive ∞ infinite value
def n = Math.abs(Infinity);
println("The absolute value of Infinity is " + n);
// Negative ∞ infinite value
def x = Math.abs(-Infinity);
println("The absolute value of -Infinity is " + x);
}
}