How to convert string into integer in the Velocity template?

uma picture uma · Jan 28, 2010 · Viewed 52.9k times · Source

I have a Velocity template file which has the data from XML. I want to convert the string into integer type.

How can I do that?

Answer

Quotidian picture Quotidian · Jan 28, 2010

Aha! Been there.

#set($intString = "9")
#set($Integer = 0)
$Integer.parseInt($intString)

Doing this uses the java underlying velocity. The $Integer variable is nothing more that a java Integer object which you can then use to access .parseInt

Edit: The above code is for demonstration. Of course there are ways to optimize it.