Re: Number format issue using assign statement
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Tuesday, March 9, 2010, 12:17:53 AM, kbutler wrote:
>
> Hi -
> I am trying to do a calculation using an assign statement. This is the
> script:
>
> <#assign total = 0/>
> <#list 1..q_client_income_rental_address_max?number as i>
Data-model variables that are numbers are received as strings in your
use-case? I assume that because you are using ?number. Not a good
situation.
> <#assign var = "q_client_income_rental_income_${i}" />
> <#if var?eval?? && var?eval?number gt 0>
> <#assign total=total+var?eval?number/></#if>
It's not related to what you are asking, but better avoid using ?eval
if you can, not to mention calling it thrice... because it's slow and
too generic. Also the whole thing could be simplified:
<#assign rentalIncome = (.vars["q_client_income_rental_income_${i}"]!0)?number />
<#if rentalIncome gt 0>
<#assign total = total + rentalIncome>
</#if>
(And if there is no negative income possible ever, then you can remove
the #if too, and hence the #assign as well... so you end up with a
single line: <#assign total = total +
(.vars["q_client_income_rental_income_${i}"]!0)?number>)
(Also note that you are making business calculations in a template...
not a good idea in most applications.)
> <#assign var = "q_client_income_rental_otherincome_${i}" />
> <#if var?eval?? && var?eval?number gt 0><#assign
> total=total+var?eval?number/></#if>
> </#list>
> ${total?c}
>
> My intention is that the result number is unformatted, i.e. for computer
> audience. But, this script always produces a formatted number.
That's very strange, if we are talking about the output of ${total?c}.
> What is interesting is that putting debug print statements inside the loop
> actually produces an unformatted result:
>
> <#assign total = 0/>
> <#list 1..q_client_income_rental_address_max?number as i>
> <#assign var = "q_client_income_rental_income_${i}" />
> <#if var?eval?? && var?eval?number gt 0><#assign
> total=total+var?eval?number/></#if>
> ${total?c}
>
> <#assign var = "q_client_income_rental_otherincome_${i}" />
> <#if var?eval?? && var?eval?number gt 0><#assign
> total=total+var?eval?number/></#if>
> ${total?c}
> </#list>
> ${total?c}
>
> the result being
> 4000
>
> 4250
> 4250
Um... These are not "formatted". So far so good, right?
> note that i = 1.
>
> I have tried various syntax in the assign statements, with no luck. Doing
> something like
>
> <#assign total= (total+var?eval?number)?c />
That's bad... makes total to be a string instead of a number. You
don't want that.
> produces an error saying that total is a SimpleScalar; using total?number
> didn't help either.
total?number does nothing if total was a number already. It's only
needed if for some unlucky reasons you receive some numerical data as
text.
> I'm rather confused about this now. Am I doing something wrong? Why
> are the debug printouts actually producing my desired result? I'm
> using FM 2.3.13.
So, those above you call the "debug output". What prints the non-debug
output and where, and how does that look?
> Thanks for any ideas -
> Kent
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev