The java.math.BigDecimal compareTo method compares this BigDecimal with another BigDecimal. If the BigDecimal is equal to the argument then 0 is returned. If the BigDecimal is less than the argument then a negative value is returned. If the BigDecimal is greater than the argument then a positive value is returned.
Code examples:
Example 1:
BigDecimal a = new BigDecimal(6); BigDecimal b = new BigDecimal(12);
int result = a.compareTo(b);
if(result == 0){ System.out.println("Both values are equal"); } else if(result == -1){ System.out.println("a is less than b"); } else { System.out.println("a is greater than b"); }
Output:
a is less than b
Example 2:
BigDecimal x = new BigDecimal("10.99"); BigDecimal y = new BigDecimal("10.98");
int result = x.compareTo(y);
if(result == 0){ System.out.println("Both values are equal"); } else if(result == -1){ System.out.println("x is less than y"); } else { System.out.println("x is greater than y"); }
Output:
x is greater than y
Package library:
The java.math package library contains the BigDecimal class and the compareTo method.
Java BigDecimal.compareTo - 30 examples found. These are the top rated real world Java examples of java.math.BigDecimal.compareTo extracted from open source projects. You can rate examples to help us improve the quality of examples.