Get the sign of a value in Java

Description

The following code shows how to get the sign of a value.

Math.signum() 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.

Example


//  www.  java  2 s .c o  m

public class Main {
  public static void main(String[] args) {
    int a = 10;
    int b = -50;
    System.out.println("signum(b) = " + Math.signum(b));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization