Re: Floating-point math

Will Glass-Husain <[email protected]>
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <CADDz7ZtaWC5qnj6FhQ=mWn-3RUHSGuanp9JPQ49cUoXT_nLiaQ@mail.gmail.com>
Hi Christopher,

We added this in Velocity 1.5.  It automatically casts between different
types of numbers as appropriate.

WILL

On Tue, Aug 14, 2012 at 11:25 AM, Christopher Schultz <
[email protected]> wrote:

> All,
>
> I seem to recall that in one of the (semi-)recent releases, Velocity
> started supporting floating-point values for mathematical operators (+,
> -, *, /) as well as comparisons (<, >, <=, >=, ==), but I can't find
> anything in the documentation that suggests that this is the case.
>
> A simple test run against Velocity 1.7 works as I expect:
>
> Here is some math:
> #set($fourAndAHalf = 4.5)
> $fourAndAHalf ($fourAndAHalf.class.name)
> #set($halfThat = $fourAndAHalf / 2)
> $halfThat ($halfThat.class.name)
> #if($fourAndAHalf > $halfThat)
> works
> #else
> confused
> #end
>
> That is, the output of this script is:
>
> "
> Here is some math:
> 4.5 (java.lang.Double)
> 2.25 (java.lang.Double)
> works
> "
>
> So, it appears that floating-point math is supported. When attempting to
> run the same template under Velocity 1.4, I get this exception:
>
> Exception in thread "main"
> org.apache.velocity.exception.ParseErrorException: Lexical error:
> org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line
> 2, column 24.  Encountered: "5" (53), after : "."
>         at
> org.apache.velocity.app.VelocityEngine.evaluate(VelocityEngine.java:286)
>         at VelocityDriver.main(VelocityDriver.java:70)
>
> It seems clear that Velocity 1.4 does not support floating-point math
> but that Velocity 1.7 does. Is it documented anywhere that 1.7 (or 1.6?)
> supports floating-point math directly in the templates?
>
> I have an old project that uses Velocity-Tools' MathTool to do all kinds
> of stuff and I'd like to remove all that for clarity, but I need to be
> reasonably confident that I can do that -- at least to a certain degree
> (e.g. simple math works, but certain operators don't support floats).
>
> Thanks,
> -chris
>
>
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.