How to convert string to integer in velocity template?

Converting the string to integer in velocity is extremely easy if we know it correct. As we might already know that the standard Java type casting is not available inside velocity template.

If we want to cast String to Integer or Float, first of all we need to create a static Integer or Float. Once we have the static Integer available, we will be able to call any static method of that Integer class.

Create static Integer:

#set( $Integer = 0 ) 

Cast String to Integer:

#set($twitterLength = $Integer.parseInt("140"))

If we try to cast the String to Integer without creating the static Integer the casting will not work. parseInt method is not available untill we create the static Integer.

Comments