ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.