Java Tutorial Java Advanced Java References

Java Math - signum() Method



The Java signum() method returns the sign of a given value. It returns zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. The method can be overloaded and it can take double and float arguments. In special cases it returns the following:

  • If the argument is NaN, then the result is NaN.
  • If the argument is positive zero or negative zero, then the result is the same as the argument.

Syntax

public static double signum(double x)
public static float signum(float x)

Parameters

x Specify a floating point value whose signum is to be returned.

Return Value

Returns zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.

Exception

NA.

Example:

In the example below, signum() method returns the sign of a given value.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.signum(10)); 
  System.out.println(Math.signum(-10)); 
  System.out.println(Math.signum(0)); 
 }
}

The output of the above code will be:

1.0
-1.0
0.0

❮ Java Math Methods