cancel
Showing results for 
Search instead for 
Did you mean: 

Bigdecimal

Former Member
0 Kudos

How do we compare 2 value attributes of type Bigdecimal?

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

Please help.Marks assured.

Accepted Solutions (1)

Accepted Solutions (1)

former_member485701
Active Participant
0 Kudos

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

Answers (2)

Answers (2)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi! Supriya,

Bigdecimal class has a method

public int compareTo(BigDecimal val)

Using this u can comapre two values of type bigdecimal.

regards,

Mithileshwar