.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.52340.8
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours