Compare two BigDecimal

int compareTo(BigDecimal val)
Compares this BigDecimal with the specified BigDecimal.
booleanequals(Object x)
Compares this BigDecimal with the specified Object for equality.

compareTo(BigDecimal val) returns:

ReturnMeaning
-1this BigDecimal is numerically less than val
0this BigDecimal is numerically equal to val
1this BigDecimal is numerically greater than val.

import java.math.BigDecimal;
 

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(-1f);
        BigDecimal second = new BigDecimal(10f);
 
 
        System.out.println(first.compareTo(second));
    }
}

The output:


-1
Home 
  Java Book 
    Essential Classes  

BigDecimal:
  1. BigDecimal class
  2. Constants for One, Ten and Zero
  3. Rounding mode
  4. Create BigDecimals
  5. Methods used to do calculation
  6. Convert BigDecimal to primitive data types
  7. Compare two BigDecimal
  8. Move decimal point
  9. Scale and precision
  10. Convert BigDecimal to String
  11. Remove the trailing zeros
  12. Convert double and long to BigDecimal
  13. Calculating Euler's number e with BigDecimal