What is the BigDecimal.signum() method in Java?
BigDecimalis an immutable, arbitrary-precision signed decimal number. It contains an arbitrary precision integer unscaled value and a 32-bit integer scale. For example, in the value 10.11, 1011 is the unscaled value, and 2 is the scale. TheBigDecimalclass provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. Read more about theBigDecimalclass here.
The signum method of the BigDecimal class retrieves the signum function of a BigDecimal value.
Syntax
public int signum()
Parameters
This method does not take any arguments.
Return value
This method returns:
-
1if the value ofBiDecimalis greater than zero. -
-1if the value ofBiDecimalis less than zero. -
0if the value ofBiDecimalis equal to zero.
Code
The example below demonstrates how to use the signum method.
import java.math.BigDecimal;class BigDecimalRemainderExample {public static void main( String args[] ) {BigDecimal val1 = new BigDecimal("10.10");BigDecimal val2 = new BigDecimal("-10.10");BigDecimal val3 = new BigDecimal("0.0");System.out.println("Signum of " + val1 + " :"+ val1.signum());System.out.println("Signum of " + val2 + " : "+ val2.signum());System.out.println("Signum of " + val3 + " : "+ val3.signum());}}
Explanation
In the above code:
-
We imported the
BigDecimalclass. -
We created three
BigDecimalobjects,val1with a value of10.10,val2with a value of-10.10, andval3with a value of0.0. -
We called the
signummethod on the createdBigDeciamlobjects.- For the
val1object, we get1as the result becauseval1> 0. - For the
val2object, we get-1as the result becauseval2< 0. - For the
val3object, we get0as the result becauseval3= 0.
- For the