# velocity-user mailing list archives

##### Site index · List index
Message view
Top
From "Bill Kaufman" <BKauf...@zaplet.com>
Subject RE: comparing monetary value
Date Thu, 17 Oct 2002 16:25:53 GMT
```> Let's take the example you mentioned of calculating the lire
> equivalent
> of a Euro value: I agree that this is purely a presentation
> issue, but
> why do I have to do this calculation server-side? Web
> use JavaScript for other purposes (eg, form data validation, fancy
> roll-overs and the like), so why not use that?
> To illustrate:
[...]
> 			document.write(euros * 1936);

That's a decent solution, but for a few things:

1) You could do just this in Velocity, without dragging in a (possibly disabled or non-existent)
JavaScript interpreter:

#set ( \$lira = \$euros * 1936 )
\$lira

2) Somewhat important, though, it's also incorrect.

The conversion is actually 1936.27 lira: you'd have to do some tricks, like,

#set ( \$lira = euros * 193627 )
#set ( \$lira = \$lira / 100 )

(Please, ignore possible overflow errors in the above code,...)

3) But, while lira values may be represented by integers, (I think) French francs are in units
of 0.05 (centimes) and German marks in units of 0.01 (pfennigs).  It's still doable in Velocity,
like,

## 6.55957 euros to the franc
#set ( \$francish = euros * 655957 )
## \$francish ~= 10000s of francs
#set ( \$francish = \$francish + 250000 )
#set ( \$sous5 = \$francish / 500000 )
## \$sous5 = 1/5ths of a sous, rounded up
#set ( \$sous = \$sous5 * 5 )
## \$sous is sous to nearest centime
#set ( \$francs = \$sous / 100 )
## \$francs is now in whole francs
#set ( \$sous = \$sous % 100 ) ## Drop the extra francs
#if ( \$sous < 10 )
#set ( \$sous = "\${sous}0" )  ## Pad to two digits
#end
\$francs.\$sous

But it's not pretty.  (Plus, I've probably got some typos/thinkos in there.)

JavaScript (or any programming language which supports floating-point numbers) isn't much
better:

var centimes = Math.round(euros * 655.957 / 5)
var francs = Math.floor((centimes * 5) / 100);
var sous = (centimes * 5) % 100;
if (sous < 10)
sous = sous + "0";
document.write(francs + "." + sous);

(There might be an easier way--I'm not thinking too deeply for this,...)

And yet, while this is still completely a display (view) issue, I do believe we've left the
range of things we can expect a DHTML programmer (or Front Page user) to handle.  I'm not
sure most would even know what "overflow" meant.  (Maybe I've just got an inflated opinion
of Java programmers vs. DHTML writers.  Others' opinions?)

This example is cooked up to only include integers, so they don't have to worry about floating-point
problems, and the code _still_ looks hideous, even in a programming language which supports
floating-point numbers or decimals.

And, no offense to the Velocity developers, but I don't think they could come up with a general
solution which would work for everyone in every user's language or currency, and which would
still be comprehensible to the likely developers.

IMO, while I do think [big] decimals would be a great addition to Velocity, I don't think
it's going to solve the general problem of the display of numbers (much less money).  That
means that, while some Velocity support or standard tools would help, they aren't going to
solve every problem: someone will have to write some Java (or similar) code to really address
this problem for each specific case.

-- Bill K.

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