Re: Number format issue using assign statement

kbutler <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>

Daniel Dekany wrote:
> 
>>
>> <#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.
> 

That is correct.  It is a fact because the data comes from the UI, and gets
overwritten with every page view. Hence the data needs conversion at some
point; but maybe never, if nothing is ever done with it. A tradeoff.


Daniel Dekany wrote:
> 
>>    <#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.)
> 

Thanks for the tip.  I tried your syntax and it worked very nicely.  I have
tried that one in the past and always got execution errors.

Per the business rules - I would normally agree,  but this is not actually
the app's primary business function; i.e. this is not an accounting package,
it is a generic document production package.  FM enables the app to support
many domains. Hence again the data represented as strings. 



Daniel Dekany wrote:
> 
>>    <#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}.
> 

P-( Yarrr...in testing your code, I pasted my original back in, and it
worked fine (i.e. this thread would not have been necessary).....  but yes,
originally that very code produced the result 4,250.  I couldn't believe
what I was seeing, and tried putting ?c anywhere I could.



Daniel Dekany wrote:
> 
>> 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?
> 

No no, this was the debug output - hence the 3 lines of output.  This was
the only case in which the output was not formatted.  The original script
output 4,250.  In other words, to make it work, I had to printout total
after each re-assignment.  

<sigh>  but I have to throw out the results from earlier, since it now
works.  What changed is that 3 idle hours passed. I don't know how to
explain in, but I guess next time I'll try a server restart.

Thanks Daniel - 
Kent

-- 
View this message in context: http://n4.nabble.com/Number-format-issue-using-assign-statement-tp1585333p1585495.html
Sent from the freemarker-user mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Download Intel&#174; 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.