Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java BigDecimal compareTo example 
 

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

 

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);
    }
  }
}

Download the source code

                         

» View all related tutorials
Related Tags: c class object objects integer method type default double decimal gd value return int if biginteger example work bigdecimal values

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.