49

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?

simhumileco
  • 31,877
  • 16
  • 137
  • 115
uma
  • 529
  • 1
  • 5
  • 7
  • you have an XSLT file that operates on an XML input document and you want to convert a field that is type xs:string into type xs:integer? – vicatcu Jan 28 '10 at 17:30
  • yes i have the xml input doucument which has the string value and i want to convert into integer – uma Jan 28 '10 at 18:23

4 Answers4

105

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Quotidian
  • 2,870
  • 2
  • 24
  • 19
  • Thanks so much for this. I was breaking my head over why `$obj.getById("23")` works where as `#set($id = "23") $obj.getById($id))` doesn't. – adarshr Apr 24 '12 at 13:59
  • 1
    works for me if I set the variable first #set($int = $Integer.parseInt($intString) ) – Jabda Sep 16 '16 at 17:40
10

If you have some control over the velocity context, here's an alternative that alleviates the need to set a variable in the Velocity template.

Context velocityContext = new Context();
velocityContext.put(Integer.class.getSimpleName(), Integer.class);

This allows you to call the static methods of java.lang.Integer in your template using $Integer.parseInt($value) and doesn't rely upon the #set having been called prior to performing the type conversion in the template.

ATG
  • 1,679
  • 14
  • 25
4

The problem with parseInt is that it throws an exception in case the string is not parseable. In case you have the NumberTool loaded into your context a better solution than parseInt is the following:

#set($intString = "009")
#set($Integer=$numberTool.toNumber($intString).intValue())

#if($Integer)
 ## ok
#else
 ## nok
#end

Sometimes the NumberTool is also loaded as $number.

However, a little drawback is, that the NumberTool simply parses the first number it finds and ignores the rest, so "123a" => 123.

buergi
  • 6,039
  • 3
  • 19
  • 15
0

Nice and easy:

#set( $stringToCast = "0" )
$number.toNumber($stringToCast)

$number is the default key name for the NumberTool, but it can be override by specifying a different name in the configuration (for example $numberTool). You have to check what name for NumberTool is used in your Velocity environment.

toNumber method returns:

the object as a Number or null if no conversion is possible

If you want to have explicite an int variable, not a Number object, you can use the intValue method on the result. So the above code will looks like this:

#set( $stringToCast = "0" )
$number.toNumber($stringToCast).intValue()

Of course, you can assign the result to another variable (for example $intVal).

So the full code can look like this:

#set( $stringToCast = "0" )
#set( $intVal = $number.toNumber($stringToCast).intValue() )
Community
  • 1
  • 1
simhumileco
  • 31,877
  • 16
  • 137
  • 115