Steve Kuo Steve Kuo - 1 year ago 362
Groovy Question

Groovy String to int

I have a

String
that represents an integer value and would like to convert it to an
int
. Is there a groovy equivalent of Java's
Integer.parseInt(String)
?

Answer Source

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

int value = "99".toInteger()

Grails

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

Integer paramValue = params.int('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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download