How to Use Math.signum() in Java


In this example, we will use Math.signum() to determine whether a number is bigger than zero.

Source Code

package com.beginner.examples;

public class SignumExample {

    public static void main(String[] args){
    	//return zero if a number is zero, 1.0 if it is greater than zero, -1.0 if it is less than zero.
    	double a = -108;
    	double b = 0;
    	double c = 20;
    	System.out.println("signum of " + a +" is: " + Math.signum(a));
        System.out.println("signum of " + b +" is: " + Math.signum(b));
        System.out.println("signum of " + c +" is: " + Math.signum(c));
    }
}

Output:

signum of -108.0 is: -1.0
signum of 0.0 is: 0.0
signum of 20.0 is: 1.0
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments