.abs()

Published Jun 18, 2021Updated Aug 24, 2022
Contribute to Docs

The Math.abs() method returns the absolute value of the argument.

Syntax

Math.abs(n)

Absolute values are the non-negative versions of the value without considering the positive/negative sign.

Example

The following example uses Math.abs() to return the absolute values of -6.5, 2, and -340.8:

class AbsoluteValue {
public static void main(String[] args) {
double x = -6.5;
int y = 2;
double z = -340.8;
System.out.println(Math.abs(x));
System.out.println(Math.abs(y));
System.out.println(Math.abs(z));
}
}

This produces the following output:

6.5
2
340.8

All contributors

Looking to contribute?

Learn Java on Codecademy