Java.math.BigInteger.signum() Method


Advertisements


Description

The java.math.BigInteger.signum() returns the signum function of this BigInteger.

Declaration

Following is the declaration for java.math.BigInteger.signum() method.

public int signum()

Parameters

NA

Return Value

This method returns -1, 0 or 1 as the value of this BigInteger is negative, zero or positive.

Exception

NA

Example

The following example shows the usage of math.BigInteger.signum() method.

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create 3 BigInteger objects
      BigInteger bi1, bi2, bi3;

      // create 3 int objects
      int i1, i2, i3;

      bi1 = new BigInteger("0");
      bi2 = new BigInteger("10");
      bi3 = new BigInteger("-10");

      // assign signum results of bi1, bi2, bi3 to i1, i2, i3
      i1 = bi1.signum();
      i2 = bi2.signum();
      i3 = bi3.signum();

      String str1 = "Signum function returns " + i1 + " for " +bi1;
      String str2 = "Signum function returns " + i2 + " for " +bi2;
      String str3 = "Signum function returns " + i3 + " for " +bi3;

      // print i1, i2, i3 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

Let us compile and run the above program, this will produce the following result −

Signum function returns 0 for 0
Signum function returns 1 for 10
Signum function returns -1 for -10

java_math_biginteger.htm

Advertisements