Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2009, 06:41 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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.

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.

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:

Code:
BigDecimal total = BigDecimal.ZERO.setScale(2);
BigDecimal paymentAmt = Payment.getPaymentAmt();
total = total.add(paymentAmt);

Last edited by ShoeNinja; 12-01-2009 at 06:51 PM. Reason: Solved.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding two Arrays Mayur New To Java 1 09-27-2009 12:49 AM
Problem with adding sanox New To Java 5 09-08-2009 12:04 PM
Adding Tiles Rubics Java Applets 1 08-03-2009 05:25 AM
Adding natep67 New To Java 2 04-22-2009 08:51 AM
[new] need help on GUI adding images counterfox AWT / Swing 5 03-19-2009 11:29 AM


All times are GMT +2. The time now is 12:18 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org