velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
Subject Re: Casting in VTL
Date Wed, 21 Feb 2001 20:30:11 GMT
Arik Levin wrote:
> 
> Hi.
> I'm new to Velocity.
> If I have :
> #set ($a = "1")
> and I want to use a's value as a number, for example :
> #set ($a = $a + 1)
> Is there any thing I can do ?


I have a much more useful answer. (Sorry bout the last one :)

VTL isn't a programming language,  and we try to keep things simple. 
Casting is one of those things we don't do, but there are plenty of
simple workarounds.  Here's one :

put into the context an Integer :

  context.put( "int", new Integer(1) );

(the value passed to the constructor doesn't matter)

now, simply use the valueOf() method to convert your String to an
Integer, with which you can do math :

  #set($a = "7")
  #set($b = $int.valueOf($a) + 10)
  $b

and output is 

17

That may help if you can't avoid setting the value up as a String in the
first place.

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com

Developing for the web?  See http://jakarta.apache.org/velocity/

Mime
View raw message