Skip to Content
avatar image
Former Member

Bigdecimal

How do we compare 2 value attributes of type Bigdecimal?

I have 2 quantity fields,i need to compare them.

Please help.Marks assured.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    avatar image
    Former Member
    Oct 25, 2007 at 01:55 PM

    Hi ,

    Following is the code, you can use:-

    Bigdecimal dec1;

    Bigdecimal dec2;

    int value=dec1.compareTo(dec2);

    if(value==1)

    system.out.println("dec1 is greater than dec2");

    if(value==-1)

    system.out.println("dec1 is less than dec2");

    if(value==0)

    system.out.println("dec1 is equal to dec2");

    regards,

    Praveen

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 25, 2007 at 01:47 PM

    Hi! Supriya,

    Bigdecimal class has a method

    public int compareTo(BigDecimal val)

    Using this u can comapre two values of type bigdecimal.

    regards,

    Mithileshwar

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 26, 2007 at 02:49 AM

    Hi Supriya,

    Use the method

    public int compareTo(BigDecimal val)

    Compares this <tt>BigDecimal</tt> with the specified

    <tt>BigDecimal</tt>. Two <tt>BigDecimal</tt> objects that are

    equal in value but have a different scale (like 2.0 and 2.00)

    are considered equal by this method. This method is provided

    in preference to individual methods for each of the six boolean

    comparison operators (&lt;, ==, &gt;, &gt;=, !=, &lt;=). The

    suggested idiom for performing these comparisons is:

    <tt>(x.compareTo(y)</tt> &lt;<i>op</i>&gt; <tt>0)</tt>, where

    &lt;<i>op</i>&gt; is one of the six comparison operators.

    @param val <tt>BigDecimal</tt> to which this <tt>BigDecimal</tt> is

    to be compared.

    @return -1, 0, or 1 as this <tt>BigDecimal</tt> is numerically

    less than, equal to, or greater than <tt>val</tt>.

    Regards

    Ayyapparaj

    Add comment
    10|10000 characters needed characters exceeded