velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Will Glass-Husain <wglasshus...@gmail.com>
Subject Re: Floating-point math
Date Tue, 14 Aug 2012 19:47:56 GMT
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 <
chris@christopherschultz.net> 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
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message