Find absolute value of float, int, double and long using Math.abs in Java
Description
The following code shows how to find absolute value of float, int, double and long using Math.abs.
Example
// ww w. java 2 s .co m
public class Main {
public static void main(String[] args) {
int i = 8;
int j = -5;
System.out.println("Absolute value of " + i + " is :" + Math.abs(i));
System.out.println("Absolute value of " + j + " is :" + Math.abs(j));
float f1 = 1.40f;
float f2 = -5.28f;
System.out.println("Absolute value of " + f1 + " is :" + Math.abs(f1));
System.out.println("Absolute value of " + f2 + " is :" + Math.abs(f2));
double d1 = 3.324;
double d2 = -9.324;
System.out.println("Absolute value of " + d1 + " is :" + Math.abs(d1));
System.out.println("Absolute value of " + d2 + " is :" + Math.abs(d2));
long l1 = 3L;
long l2 = -4L;
System.out.println("Absolute value of " + l1 + " is :" + Math.abs(l1));
System.out.println("Absolute value of " + l2 + " is :" + Math.abs(l2));
}
}
The code above generates the following result.