Results 1 to 5 of 5
  1. #1
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default Adding BigDecimals

    I'm trying to add several BigDecimals in a for loop. For some reason, I'm getting 0 on the other end of the loop.

    Java Code:
    BigDecimal total = BigDecimal.ZERO.setScale(2);
    for(Payment payment : payments) {
    	BigDecimal payAmt = payment.getPaymentAmt();
    	total.add(payAmt);
    }
    While debugging, I see that in the BigDecimal add method, I'm hitting this exit point.

    Java Code:
    boolean lhsIsZero = lhs.signum() == 0;
    boolean augendIsZero = augend.signum() == 0;
    
    if (lhsIsZero || augendIsZero) {
    	int preferredScale = Math.max(lhs.scale(), augend.scale());
    	BigDecimal result;
    
    // Could use a factory for zero instead of a new object
    if (lhsIsZero && augendIsZero)
            return new BigDecimal(BigInteger.ZERO, 0, preferredScale);
    .sugnum() is not 0 for both the operands when I look at them just before add is called so I'm not sure why I would hit this return statement.

    Any ideas?


    EDIT:

    BigDecimal.add() returns a BigDecimal. So it has to be something like:

    Java Code:
    BigDecimal total = BigDecimal.ZERO.setScale(2);
    BigDecimal paymentAmt = Payment.getPaymentAmt();
    total = total.add(paymentAmt);
    Last edited by ShoeNinja; 12-01-2009 at 05:51 PM. Reason: Solved.

  2. #2
    leet3lite is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    What was the solution?

    I am having the same problem.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Read the post.
    They gave the solution at the end...

  4. #4
    leet3lite is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    I read the whole post, but didn't understand it. The code at the top has a different algorithm and semantic logic to the second.
    But I finally figured out what he was trying to say: BigDecimal is immutable (of course!), so you have to re-assign the value. The code therefore would have to look like this:

    Java Code:
    BigDecimal total = BigDecimal.ZERO.setScale(2);
    for(Payment payment : payments) {
    	BigDecimal payAmt = payment.getPaymentAmt();
    	[B][COLOR="Red"]total = [/COLOR][/B]total.add(payAmt);
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

Similar Threads

  1. Adding two Arrays
    By Mayur in forum New To Java
    Replies: 1
    Last Post: 09-26-2009, 11:49 PM
  2. Problem with adding
    By sanox in forum New To Java
    Replies: 5
    Last Post: 09-08-2009, 11:04 AM
  3. Adding Tiles
    By Rubics in forum Java Applets
    Replies: 1
    Last Post: 08-03-2009, 04:25 AM
  4. Adding
    By natep67 in forum New To Java
    Replies: 2
    Last Post: 04-22-2009, 07:51 AM
  5. [new] need help on GUI adding images
    By counterfox in forum AWT / Swing
    Replies: 5
    Last Post: 03-19-2009, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •