Search code examples
javamathprimitive

Equivalent function for Math.signum(double) for int or long in Java


Is there an equivalent function for Math.signum(double) or Math.signum(float) in Java for other primitive numbers like int or long. I don't want to write code like

  int sign = (int) Math.signum((double) intValue);

when there is a better alternative.


Solution

  • You can use it this way:

    Integer.signum(int i) and Long.signum(long l)

    Link to javadoc: https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#signum-int-