Java BigDecimal signum() Method with Example

BigDecimal Class signum() method: Here, we are going to learn about the signum() method of BigDecimal Class with its syntax and example.
Submitted by Preeti Jain, on May 07, 2020

BigDecimal Class signum() method

  • signum() method is available in java.math package.
  • signum() method is used to get the signum (sign number) function of this BigDecimal object.
  • signum() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • signum() method does not throw an exception at the time of returning signum of this BigDecimal.

Syntax:

    public int signum();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it may return anyone of these three values [-1, 1 or 0] based on this BigDecimal.

  • If this BigDecimal holds value less than 0, it returns -1.
  • If this BigDecimal holds value greater than 0, it returns 1.
  • If this BigDecimal holds value equal to 0, it returns 0.

Example:

// Java program to demonstrate the example 
// of int signum() method of BigDecimal

import java.math.*;

public class SignumOfBD {
    public static void main(String args[]) {
        // Initialize three variables of int and 
        // String type
        int val1 = 1245;
        int val2 = 0;
        String str = "-100";

        // Initialize three BigDecimal objects  
        BigDecimal b_dec1 = new BigDecimal(val1);
        BigDecimal b_dec2 = new BigDecimal(str);
        BigDecimal b_dec3 = new BigDecimal(val2);

        // return the signum (sign number) of
        // this BigDecimal b_dec1 (1245) so it
        // returns 1 because b_dec1 holds positive
        // value
        int signum = b_dec1.signum();
        System.out.println("b_dec1.signum(): " + signum);

        // returns the signum (sign number) of
        // this BigDecimal b_dec2 (-100) so it
        // returns -1 because b_dec2 holds negative
        // value
        signum = b_dec2.signum();
        System.out.println("b_dec2.signum(): " + signum);

        // returns the signum (sign number) of
        // this BigDecimal b_dec3 (0) so it
        // returns 0 because b_dec3 holds value 0
        signum = b_dec3.signum();
        System.out.println("b_dec3.signum(): " + signum);
    }
}

Output

b_dec1.signum(): 1
b_dec2.signum(): -1
b_dec3.signum(): 0



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.