Saturday, October 25, 2025
HomeLanguagesJavaBigDecimal max() Method in Java

BigDecimal max() Method in Java

The java.math.BigDecimal.max(BigDecimal val) method in Java is used to compare two BigDecimal values and return the maximum of the two. This is opposite to BigDecimal max() method in Java.

Syntax:

public BigDecimal max(BigDecimal val)

Parameters: The function accepts a BigDecimal object val as parameter whose value is compared with that of this BigDecimal object and the maximum value is returned.

Return Values: This method returns the BigDecimal whose value is the greater of this BigDecimal and val. In case if both are equal, this BigDecimal is returned.

Examples:

Input :  a = 17.000041900, b = 17.0000418999
Output : 17.000041900

Input : a = 235900000146, b = 236000000000
Output : 236000000000

Below programs will illustrate max() function of BigDecimal class.

Program 1:




// Java program to illustrate use of
// BigDecimal max() function in Java      
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create 2 BigDecimal objects
        BigDecimal a, b;
  
        a = new BigDecimal("235900000146");
        b = new BigDecimal("236000000000");
  
        // print the maximum value
        System.out.println("Maximum Value among " + a + 
                       " and " + b + " is " + a.max(b));
    }
}


Output:

Maximum Value among 235900000146 and 236000000000 is 236000000000

Program 2:




// Java program to illustrate use of BigDecimal max() 
// to display maximum length among two strings in Java  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create 2 BigDecimal objects
        BigDecimal a, b;
        String s = "Lazyroar";
        String str = "GeeksClasses";
  
        int l1, l2;
        l1 = s.length();
        l2 = str.length();
  
        a = new BigDecimal(l1);
        b = new BigDecimal(l2);
  
        // Print the respective lengths
        System.out.println("Length of string " + s + " is " + a);
        System.out.println("Length of string " + str + " is " + b);
          
        // Print the maximum value
        System.out.println("Maximum length is " + a.max(b));
    }
}


Output:

Length of string Lazyroar is 13
Length of string GeeksClasses is 12
Maximum length is 13

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#max()

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS