velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Revusky <revu...@wanadoo.es>
Subject Re: comparing monetary value
Date Wed, 16 Oct 2002 16:34:26 GMT
On Wednesday 16 October 2002 05:03 pm, Christoph.Reck@dlr.de wrote:
> 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

Still seems rather tortuous. In FreeMarker, you could just write:

DEM #{1.95583 * priceInEuros  ; m2M2}

The m2M2 thingy is saying to use a minimum and maximum of 2 digits after the 
decimal. (i.e. exactly 2 digits.)

The resulting output would be something like 

DEM 11.79

or 

DEM 11,79 

depending on the page's locale setting.

Well, to make sure you had the write display, you could go:

<setting locale="de_DE">

Der Preis ist: DEM #{1.95583 * priceInEuros ; m2M2}


>
> 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

:-)

This reminds of a saying: "Damn! If I had some ham, I could have a ham 
sandwich -- if I had some bread."

GrĂ¼ssen,

Jonathan Revusky
--
FreeMarker-Velocity comparison doc
http://freemarker.sourceforge.net/fmVsVel.html
Velocity->FreeMarker template conversion utility
http://freemarker.sourceforge.net/usCavalry.html


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


Mime
View raw message