BigDecimal Comparison

In this chapter you will learn:

  1. How to compare two BigDecimal values
  2. A demo to use to compareTo() method on BigDecimal

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.

Use compareTo method

import java.math.BigDecimal;
 /* java2 s .co  m*/

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:

Next chapter...

What you will learn in the next chapter:

  1. How to Convert BigDecimal to String
Home » Java Tutorial » Data Types

Java Primitive Data Types
Java byte type
Java byte type conversion
Java short type
Java short type conversion
Java int type
Java int type conversion
Java long type
Java long type conversion
Java float type
Java float type conversion
Java double type
Java double type creation and comparison
Java double type conversion
Java Automatic Type Conversion and Casting
Data type casting
Java type promotion
Java char type
Java char conversion
Java char value and its attributes
Java boolean type
Java boolean type conversion
Autoboxing and auto-unboxing
Java Array
Create an Array
Array Index and length
Multidimensional Arrays
Array examples
Array copy
Array compare
Array Binary search
Array sort
Array to List
Array fill value
Array to String
BigInteger class
BigInteger creation
BigInteger add, subtract, multiply and divide
BigInteger power and modPow
BigInteger conversion
BigInteger to String
BigInteger bit and,or,xor,test,flip,negate
BigInteger bit shift left and right
BigInteger prime value
BigDecimal
BigDecimal constants
BigDecimal Rounding mode
BigDecimal creation
BigDecimal calculation
BigDecimal convert
BigDecimal Comparison
BigDecimal to String
BigDecimal decimal point
BigDecimal precision
BigDecimal format
Currency class