# velocity-user mailing list archives

##### Site index · List index
Message view
Top
From Christoph.R...@dlr.de
Subject Re: comparing monetary value
Date Wed, 16 Oct 2002 15:03:57 GMT
```I'm +1 for float or double support with velocity operators.

In the case of currencies, it would be simple to add a macro
like:
#macro( local_currency \$currencyCode \$price )
## ensure that the localy used variables do not conflict with any globals
## (in this case we could have used #local() - without parameters)
#local( \$factor \$currency \$currencyStr \$fraction )
## do something smart here to convert the \$currencyCode to a factor...
#set( \$conversionRate = 1.95583 )
## ensure \$price is in a floating point format using the Double.valueOf() method
## properly round the 3rd digit after the comma
#set( \$currency = ( \$conversionRate * \$conversionRate.valueOf("\$price") ) + 0.005 )
## ensure the string representation has at least two traling zeros
#set( \$currencyStr = "\${currency}00"
## get the last index of the fraction digits
#set( \$fraction = \$currencyStr.indexOf('.') + 3 )
## emit the new string
#text( "\$currencyCode \$currencyStr.substring(0, \$fraction)" )
#end
#end

then:
#local_currency( 'DEM' 6.03 )
emits:
DEM 11.79

The above macro would work if there would be:
1. float support
2. a dircetive called #local(...)
3. a simple #text macro that does not emit surrounding whitespace.
4. and whitespace gobbling would work properly

Tim Colson wrote:
> Jonathan wrote:
>
>>The price is: \$price euros.
>>The price is: \$price euros. (\${price*6.556 FFR)
>>What you seem to be implying is
>>that the non-technical people who want to introduce this
>>change should have to talk to the programmers so that they expose the
>
> constant
>
>>in the template.
>>For some reason, it is "bad" (TM) for them to write 6.556
>>literally in the  template.
>
>
> I do not assume 'implied things' since that puts words in peoples
> mouths.
>
> So, I will explicitly say that I personally would indeed expect a
> non-technical designer to ask the Developer for this feature, rather
> than hack it in directly because the template system made it possible.
>
> "some reason": I can propose several reasons why this situation could be
> 1) the conversion rate changes tomorrow to 5.998
>
> 2) each user has customized preferences for selecting her preferred
> alternative currency (FFR, DM, USD, etc.)
>
> 3) the result of \$price * \$conversionRate will still need to have the
> correct rounding and formatting for the resulting currency. (Ex. DM 6,03
> or \$6.03 USD - not \$6.0029999987 DM)
>
> Summary: my solution, using Velocity, would be to do this work in the
> controller and give the template designer simply \$price and
> \$preferredLocalPrice.
>
> Cheers,
> Tim
>
>
> --
> To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>
>
>

--
:) Christoph Reck

--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>