Java abs() Method
abs() returns the absolute value of the parameter. The parameter can be of type int, float, long, double, short, byte.
Syntax
The method format for each type is similar to the following:
double abs(double d) float abs(float f) int abs(int i) long abs(long lng)
Parameters
- Any primitive data type.
Return Value
Returns the absolute value of the parameter.
Example
Example
public class Test{
public static void main(String args[]){
Integer a = -8;
double d = -100;
float f = -90f;
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}
}
public static void main(String args[]){
Integer a = -8;
double d = -100;
float f = -90f;
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}
}
Compile the above program, the output result is:
8 100.0 90.0
Other Extensions
Java Number Class