What is the BigDecimal.max() 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 max method of the BigDecimal class returns the maximum of the current object and the passed argument.
Syntax
public BigDecimal max(BigDecimal val)
Parameters
The max method takes a BigDecimal object as a parameter.
Return value
-
If the parameter passed is greater than the current object value, then the
maxmethod returns the parameter. Otherwise, it returns the current object. -
If the parameter and current object are equal, then
maxreturns either one.
Code
The example below demonstrates how to use the max method.
import java.math.BigDecimal;class BigDecimalMaxExample {public static void main( String args[] ) {BigDecimal val1 = new BigDecimal("99.01");BigDecimal val2 = new BigDecimal("99.022");BigDecimal result = val1.max(val2);System.out.println(result);}}
Explanation
In the code above, we do the following:
-
Import the
BigDecimalclass. -
Create two
BigDecimalobjects,val1with value99.01andval2with value99.022. -
Call the
maxmethod on theval1object withval2as an argument. This method call will return theval2object as a result becauseval2>val1.