Absolute Value Using Java Math

Compiled By Unknown - No Comments
An absolute value is the number with no negative sign. If the number is already positive, no change is made. We first import java.lang.Math. Types: Math.abs returns various types. These depend on the types passed to it. If we pass an int, we receive an int in return.

Based on:

Java 7

Java program that uses Math.abs

import java.lang.Math;

public class Program {
    public static void main(String[] args) {

 // This version uses an int.
 int value = Math.abs(-1);
 System.out.println(value);

 // This version uses a double.
 double value2 = Math.abs(-1.23);
 System.out.println(value2);

 int value3 = Math.abs(1);
 System.out.println(value3);
    }
}

Output

1
1.23
1


1. absolute value using java Math Class

Syntax:-

Math.abs(variable)

Here variable can be double,float,int and long datatype.

abs method is used to find absolute value of variable.

Math.abs(-2.3) = 2.3

Tags:

No Comment to " Absolute Value Using Java Math "