You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Jeremy Cowgar <jc...@cowgar.com> on 2004/05/26 22:28:24 UTC

Doing math

Greetings.

I have a object with that returns a BigDecimal. I would like to keep a 
running tally and present a total at the end. Here's some example code:

#set ($totalAmount = 0)
#foreach ($item in $items)
<tr><td>$item.name</td><td>$item.amount</td></tr>
#set ($totalAmount = $totalAmount + $item.amount)
#end
<tr><th>Total:</th><td>$totalAmount</td></tr>

How do I accomplish this task? $item.amount is appearing on the screen 
correctly, but $totalAmount always equal's zero. Even inside the #foreach

Thanks!

Jeremy



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Doing math

Posted by Will Glass-Husain <wg...@forio.com>.
Velocity 1.4 only does Integer arithmetic.  There's a patch out there to do
decimal arithmetic, and rumor has it that the patch will make it into 1.5.
But it's not clear when that might be.

If you're feeling adventurous, try downloading the CVS source for Velocity
and applying the patch.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=25874

Best,
WILL

----- Original Message ----- 
From: "Jeremy Cowgar" <jc...@cowgar.com>
To: <ve...@jakarta.apache.org>
Sent: Wednesday, May 26, 2004 1:28 PM
Subject: Doing math


> Greetings.
>
> I have a object with that returns a BigDecimal. I would like to keep a
> running tally and present a total at the end. Here's some example code:
>
> #set ($totalAmount = 0)
> #foreach ($item in $items)
> <tr><td>$item.name</td><td>$item.amount</td></tr>
> #set ($totalAmount = $totalAmount + $item.amount)
> #end
> <tr><th>Total:</th><td>$totalAmount</td></tr>
>
> How do I accomplish this task? $item.amount is appearing on the screen
> correctly, but $totalAmount always equal's zero. Even inside the #foreach
>
> Thanks!
>
> Jeremy
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Doing math

Posted by Nathan Bubna <na...@esha.com>.
Jeremy Cowgar said:
> I have a object with that returns a BigDecimal. I would like to keep a
> running tally and present a total at the end. Here's some example code:
>
> #set ($totalAmount = 0)
> #foreach ($item in $items)
> <tr><td>$item.name</td><td>$item.amount</td></tr>
> #set ($totalAmount = $totalAmount + $item.amount)
> #end
> <tr><th>Total:</th><td>$totalAmount</td></tr>
>
> How do I accomplish this task? $item.amount is appearing on the screen
> correctly, but $totalAmount always equal's zero. Even inside the #foreach

http://java.sun.com/j2se/1.3/docs/api/java/math/BigDecimal.html#add(java.math.BigDecimal)

or, if you prefer,

http://jakarta.apache.org/velocity/tools/generic/MathTool.html

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Doing math

Posted by Mike Kienenberger <mk...@alaska.net>.
Jeremy Cowgar <jc...@cowgar.com> wrote:
> Greetings.
> 
> I have a object with that returns a BigDecimal. I would like to keep a 
> running tally and present a total at the end. Here's some example code:
> 
> #set ($totalAmount = 0)
> #foreach ($item in $items)
> <tr><td>$item.name</td><td>$item.amount</td></tr>
> #set ($totalAmount = $totalAmount + $item.amount)
> #end
> <tr><th>Total:</th><td>$totalAmount</td></tr>
> 
> How do I accomplish this task? $item.amount is appearing on the screen 
> correctly, but $totalAmount always equal's zero. Even inside the #foreach

I generally compute the total in my java code and stick the total in my 
context.
I don't think VTL can handle operator math on BigDecimal objects.

Remember that if you're doing something like BigDecimalA.add(BigDecimalB) 
that BigDecimalA remains constant -- the result of the addition is only 
available via the add method return value.


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org