cancel
Showing results for 
Search instead for 
Did you mean: 

Big Decimal

Former Member
0 Kudos

Hi Group,

When I am using BigDecimal in the Following code I am getting the following problems:


BigDecimal c1 = new BigDecimal(a);
BigDecimal c2 = new BigDecimal(b);
BigDecimal c3 = new BigDecimal(c);
BigDecimal c4 = new BigDecimal(d);
BigDecimal c5 = new BigDecimal(e);
BigDecimal c6 = new BigDecimal(f);
BigDecimal c7 = new BigDecimal(g);
BigDecimal c8 = new BigDecimal(h);

BigDecimal sum;
BigDecimal sum1;
BigDecimal sum2;
BigDecimal sum3;
BigDecimal sum4;
BigDecimal sum5;
BigDecimal sum6;
BigDecimal  sum7;

sum1 = c1.add(c2);
sum2 = sum1.add(c3);
sum3 = sum2.add(c4);
sum4 =sum3.add(c5);
sum5=sum4.add(c6);
sum6=sum5.add(c7);

sum  = sum6;

//sum = c1+c2+c3+c4+c5+c6+c7;


if (sum == c8)
{
return("true");
}
else
{
writetrace.addWarning("Sum of amounts does not match with amount in End of File");
return ("false");
}

actually the Sum,C8 values are same but when I am using above the code it is giving the False exception,can any body suggest

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Let me give you a UDF for two. Create a <b>VALUE</b> user defined function <b>SUMBIGDECIMAL</b> with arguments <b>a</b> and <b>b</b>. Then import the package java.math.BigDecimal and add the following code:

{

BigDecimal bigA = new BigDecimal(a);

BigDecimal bigB = new BigDecimal(b);

BigDecimal bigSum = bigA.add(bigB);

return bigSum.toString();

}

If you pass the input as 999999999999999 and 999999999999999. Then you will have the output with this UDF as 1999999999999998.

Similarly you can extend with the number of inputs.

---Satish

Former Member
0 Kudos

Hi,

When you are using == your are comparing the references not the values inside objects. Objects sum and c8 are always different, they have only the same value. Here is correct code (you need to use method equals).

<b>if (sum.equals(c8))</b>

{

return("true");

}

else

{

writetrace.addWarning("Sum of amounts does not match with amount in End of File");

return ("false");

}

Regards,

Wojciech

Reminder: Please reward points for good answer.

Former Member
0 Kudos

Hi Wojciech ,

Thank you .It Solved

Former Member
0 Kudos

You are welcome

prabhu_s2
Active Contributor
0 Kudos

hi

am not a java expert but eager to know on what u pointed out:

<b>When you are using == your are comparing the references not the values inside objects</b>

what is the reference for sum and c8 ...can u pls elobrate on this

Former Member
0 Kudos

Hi,

Sum and c8 are references to objects(it something like a pointer to object with unique object id). Try to imagine car class. You can have 2 red toyotas with the same equipment, they are equal but are not the same objects of class cars. The same concept is in almost all programming languages.

Regards,

Wojciech

henrique_pinto
Active Contributor
0 Kudos

> hi

>

> am not a java expert but eager to know on what u

> pointed out:

>

> <b>When you are using == your are comparing the

> references not the values inside objects</b>

>

> what is the reference for sum and c8 ...can u pls

> elobrate on this

Hey Prabhu,

in Java, the == operator implements one thing when comparing Objects (implemented by classes) and another thing when comparing primitive types (such as int, double, char, boolean etc).

If you do (Object) o1 == (Object) o2 you are comparing two objects and they will almost never be equal (there are some exceptions, though), even though they have the same value (as in the BigDecimal case). If you do (int) i1 == (int) i2, it will compare the value since the value is the only reference the jvm has for that primitive type.

To solve this issue, the Object class (which all other classes extend) has a .equals() method. All classes should implement this .equals() method in their definition, so that when someone want to compare 2 objects, they will know if they have same value. For example, the .equals() method for the String class could be something like:


public String () {
...
  public boolean equals (String s) {

    if (this.length() == s.length()) {
      for (int i = 0; i < this.length(); i++) {
        if (this.chartAt(i) != s.chartAt(i)) {
          return false;
        }
      }
    } else {
      return false;
    }
    return true;
  }
...
}

If you don't implement the .equals() method in your class, then the default implementation of the Object class will be used. However the default implementation is the == operator, thus it is always recomended to implement the equals() method in your new class.

Regards,

Henrique.

prabhu_s2
Active Contributor
0 Kudos

try to print the values of sum and c8 and see if both are the same.