Java BigDecimal compareTo example

BigDecimal class compareTo() method analyzes this.object value with the specified object value numerically.

Tutorials

Java BigDecimal compareTo example

BigDecimal class compareTo() method analyzes this.object value with the specified object value  numerically. This means method numerically compares the value of the object on which it is invoked, with the value of the mentioned object. During this method process, the object values are compared to each other numerically. Method return type is int therefore it returns only integer values.

It returns negative value -1, number zero 0 & positive value +1. Now, negative value indicates that specified object is numerically greater than object on which the method is invoked. Positive value signifies that the mentioned object is numerically lesser than object on which the method is invoked. And finally number zero means both object values are numerically equal.

More simply -1 indicates that this.object is lesser than the specified object numerically. 0 (zero) means this. object value is numerically equal with the specified object value, & +1 means this.object value is numerically greater than the specified object value.

Syntax for using the method:

System.out.println(bigdecimal_objectName.compareTo(BigDecimal x));
or
int x = this.object.compareTo(x);

Having numerical equal values does not states that the scales defined for the objects value will also match with each other. The scale difference is not undertaken into account for observation by the method so, no error is generated regarding scales topic. Method throws NumberFormatException if it finds a value other than a integer or double value.

Java_BigDecimal_compareTo.java

 ``` import java.math.MathContext; import java.math.BigDecimal; import java.text.NumberFormat; import java.io.*; import javax.swing.*; public class Java_BigDecimal_compareTo {   public static void main(String args[]) {   String val_0, val_1;   val_0 = val_1 = " ";   val_0 = JOptionPane.showInputDialog(null, "obj_0",   "roseindia input box", 1);   val_1 = JOptionPane.showInputDialog(null, "obj_1",   "roseindia input box", 1);   BigDecimal obj_0 = new BigDecimal(val_0),   obj_1 = new BigDecimal(  val_1);   String message = " ";   System.out.println("BigDecimal objects obj_0 & " +   "obj_1");   if (obj_0.compareTo(obj_1) == -1) {   message = "obj_0 : "   + obj_0.toString()   + "\nobj_1 : "   + obj_1.toString()   + "\ncompareTo method result : "   + obj_0.compareTo(obj_1)   + "\nThis shows that obj_0 object is " +   "numerically less than object obj_1";   JOptionPane.showMessageDialog(null, message,   "roseindia message box", 3);   System.out.println(message);   } else if (obj_0.compareTo(obj_1) == 1) {   message = "purab : "   + obj_0.toString()   + "\nobj_1 : "   + obj_1.toString()   + "\ncompareTo method result : "   + obj_0.compareTo(obj_1)   + "\nThis shows that obj_0 object is " +   "numerically greater than object obj_1";   JOptionPane.showMessageDialog(null, message,   "roseindia message box", 3);   System.out.println(message);   } else {   message = "purab : " + obj_0.toString()    + "\nobj_1 : "   + obj_1.toString()   + "\ncompareTo method result : "   + obj_0.compareTo(obj_1)   + "\nThis shows that both objects are " +   "numerically equal";   JOptionPane.showMessageDialog(null, message,   "roseindia message box", 3);   System.out.println(message);   }   } }```

Java BigDecimal compareTo example

Related Tutorials

Discuss: Java BigDecimal compareTo example

Subject (*):