You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Arik Levin <ar...@trendline.co.il> on 2001/02/21 19:46:40 UTC

Casting in VTL

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 ?

TIA
Arik.


Re: Casting in VTL

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
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 ?

Not really.  can you set a's value to 1 a la

#set($a = 1 )

?

-- 
Geir Magnusson Jr.                               geirm@optonline.com

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

Re: Casting in VTL

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
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/