You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ganesh <ga...@wipro.com> on 2003/11/19 10:48:48 UTC
Comparing Floating point variables in VTL
Hi All,
Is it possible to use Floating point variables in VTL? If that is
possible can use do comparison something like the following...
#macro(addSymbol $amt)
#if( $amt < 1)<$amt#else$amt#end
#end
The above macro adds a '<' symbol it the value is less than 1. In the
following set statement, if I try to set the value of a variable to 0.5
it gives a Error:- TokenMgrError: Lexical error encountered 5 after :
"."
#set( $perCash=0.5 )
Regards,
Ganesh
Confidentiality Notice
The information contained in this electronic message and any attachments to this message are intended
for the exclusive use of the addressee(s) and may contain confidential or privileged information. If
you are not the intended recipient, please notify the sender at Wipro or Mailadmin@wipro.com immediately
and destroy all copies of this message and any attachments.
RE: Comparing Floating point variables in VTL
Posted by Tim Colson <tc...@cisco.com>.
> > Is it possible to use Floating point variables in VTL? If that is
> > possible can use do comparison something like the following...
As Will said, not currently, but probably at some point. :-)
An alternative to consider:
If there is a goal to keep business logic out of the templates (i.e.
MVC separation), then numerical comparisons of any sort (float/int)
sometimes contain business logic that could be decided in the backend.
> > #macro(addSymbol $amt)
> > #if( $amt < 1)<$amt#else$amt#end
> > #end
> >
> > The above macro adds a '<' symbol it the value is less than 1
Obviously I do not know much of the requirements for this template
snippet, so I'll make some up. :-)
Maybe "$amt < 1" could be described as "Check if the amount_of_something
is less than a defined_minimum_limit which is currently set to 1.0".
The back-end controller could perform that test and put the result in a
boolean context var: $AMT_IS_LESS_THAN_MIN_LIMIT
context.put("AMT_IS_LESS_THAN_MIN_LIMIT", new Boolean(myAmount <
minLimitFromDB));
New Macro:
#macro(addSymbol $amt $AMT_IS_LESS_THAN_MIN_LIMIT)
#if( $AMT_IS_LESS_THAN_MIN_LIMIT)<#end$amt
#end
If the logic/rule changes and defined_minimum_limit becomes 0.55555 -
the View template(s) would work without modification.
I'm not trying to preach MVC, just give an alternate solution. But
please, do whatever works best for you. :-)
Cheers,
Timo
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Comparing Floating point variables in VTL
Posted by Will Glass-Husain <wg...@forio.com>.
Hi Ganesh,
It's currently not possible to do floating point comparison in VTL, but I've
checked a patch into Bugzilla that permits this and I expect you'll see it
in the 1.5 release.
In the meantime, you can write a simple object that performs comparisons,
arithmetic, etc on floating point numbers. I have a library with functions
such as:
public boolean equals(Object a1, Object a2)
{
return parseDouble(a1) == parseDouble(a2);
}
public boolean greaterthan(Object a1, Object a2)
{
return parseDouble(a1) > parseDouble(a2);
}
Where the method parseDouble converts a String or Double into a double.
WILL
----- Original Message -----
From: "Ganesh" <ga...@wipro.com>
To: <ve...@jakarta.apache.org>
Sent: Wednesday, November 19, 2003 1:48 AM
Subject: Comparing Floating point variables in VTL
>
> Hi All,
> Is it possible to use Floating point variables in VTL? If that is
> possible can use do comparison something like the following...
>
>
> #macro(addSymbol $amt)
> #if( $amt < 1)<$amt#else$amt#end
> #end
>
> The above macro adds a '<' symbol it the value is less than 1. In the
> following set statement, if I try to set the value of a variable to 0.5
> it gives a Error:- TokenMgrError: Lexical error encountered 5 after :
> "."
>
> #set( $perCash=0.5 )
>
> Regards,
> Ganesh
>
>
> Confidentiality Notice
>
> The information contained in this electronic message and any attachments
to this message are intended
> for the exclusive use of the addressee(s) and may contain confidential or
privileged information. If
> you are not the intended recipient, please notify the sender at Wipro or
Mailadmin@wipro.com immediately
> and destroy all copies of this message and any attachments.
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Comparing Floating point variables in VTL
Posted by Nathan Bubna <na...@esha.com>.
Ganesh said:
> Is it possible to use Floating point variables in VTL?
...
> In the
> following set statement, if I try to set the value of a variable to 0.5
> it gives a Error:- TokenMgrError: Lexical error encountered 5 after :
> "."
>
> #set( $perCash=0.5 )
http://jakarta.apache.org/velocity/tools/generic/MathTool.html
Nathan Bubna
nathan@esha.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Comparing Floating point variables in VTL
Posted by Jonathan Revusky <re...@wanadoo.es>.
Ganesh wrote:
> Hi All,
> Is it possible to use Floating point variables in VTL? If that is
> possible can use do comparison something like the following...
>
>
> #macro(addSymbol $amt)
> #if( $amt < 1)<$amt#else$amt#end
> #end
>
> The above macro adds a '<' symbol it the value is less than 1. In the
> following set statement, if I try to set the value of a variable to 0.5
> it gives a Error:- TokenMgrError: Lexical error encountered 5 after :
> "."
>
> #set( $perCash=0.5 )
Currently, Velocity has no support for manipulation of decimal numbers
in the template. Apparently, they are working on it, but it is taking a
very long time, since discussion has been going on for at least a year.
If you do need this functionality right now, I think it's fair to point
out that another template engine has had this functionality for a long
time. FreeMarker is a template language much like Velocity that does
support the use of decimal numbers in templates.
It is not terribly hard to convert templates from VTL to FTL (FreeMarker
Template language) since we have an automatic conversion utility that
does most of the work. OTOH, you can wait for the Velocity developers to
implement decimal numbers in some future release. It's your choice.
Regards,
Jonathan Revusky
--
lead developer, FreeMarker project, http://freemarker.org/
FM-Vel comparison page: http://freemarker.org/fmVsVel.html
>
> Regards,
> Ganesh
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org