Steve Kuo Steve Kuo - 1 year ago 136
Groovy Question

Groovy String to int

I have a

that represents an integer value and would like to convert it to an
. Is there a groovy equivalent of Java's

Answer Source

Use the toInteger() method to convert a String to an Integer, e.g.

int value = "99".toInteger()


If you're converting a request parameter in a Grails controller, there's an even better way

Integer paramValue ='paramName')

One of the nice features of this technique is that it's safe, i.e. if the parameter cannot be converted to an Integer it returns null rather than throwing an exception.