Java .signum()
Anonymous contributor
Published Oct 31, 2022
Contribute to Docs
The Math.signum() method determines whether a number is positive, negative, zero, or NaN.
Syntax
Math.signum(num)
The .signum() method takes a num parameter and returns a double value that indicates whether num is positive, negative, zero, or NaN.
Some cases to consider include the following:
- If
numis positive, a positive non-zero value is returned. - If
numis negative, a negative non-zero value is returned. - If
numis zero, zero is returned. - If the value of
numisNaN,NaNis returned.
Example
The following example demonstrates the .signum() method and its many use cases:
// Test.javapublic class Test {public static void main(String[] args) {System.out.println(Math.signum(8.75));System.out.println(Math.signum(-3.5));System.out.println(Math.signum(0));System.out.println(Math.signum(Double.NaN));}}
This will produce the following output:
1.0-1.00NaN
All contributors
- Anonymous contributor
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours