What is the BigDecimal.min() method in Java?
BigDecimalis an immutable, arbitrary-precision signed decimal number.BigDecimalcontains 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 min method of the BigDecimal class returns the minimum value of the current object and the passed argument.
Syntax
public BigDecimal min(BigDecimal val)
Parameters
The min method takes a BigDecimal object as a parameter.
Return value
-
If the parameter is less than the current object value, then
minreturns the parameter. Otherwise, the method returns the current object. -
If the parameter and current object are equal, then the
minmethod returns either one.
Code
The example below demonstrates how to use the min method.
import java.math.BigDecimal;class BigDecimalMinExample {public static void main( String args[] ) {BigDecimal val1 = new BigDecimal("99.01");BigDecimal val2 = new BigDecimal("99.022");BigDecimal result = val1.min(val2);System.out.println(result);}}
Explanation
In the code above, we:
-
Import the
BigDecimalclass. -
Create two
BigDecimalobjects,val1with value99.01andval2with value99.022. -
Call the
minmethod on theval1object withval2as an argument. This method call will return theval1object as a result becauseval1<val2.