cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script convert string to integer

SOLVED
Occasional Contributor

Groovy Script convert string to integer

latest = "123123123"

int number = latest.toInteger()

 

I am trying to convert this string to integer, tried various options from internet.

I always get "java.lang.NumberFormatException", but not converting to integer

 

Let mw know any possible answers

4 REPLIES
Community Leader

Re: Groovy Script convert string to integer

Hi @subhaD,

 

It's working fine for me.

 

Capture.PNG

Can your share more details or screenshot where you are getting this error.

 

 

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

 

Thanks and Regards,

Himanshu Tayal

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Occasional Contributor

Re: Groovy Script convert string to integer

Thanks so much for replying @HimanshuTayal

 

This is my exact script.

I have converted CCN to string and then trying to make it an integer

 

Highlighted
Community Leader

Re: Groovy Script convert string to integer

Your number is too large for the integer data type, see the following for more details:

 

http://docs.groovy-lang.org/docs/next/html/documentation/core-syntax.html#_numbers

 

You will need to use the "Long" data type instead:

 

def numberString = '1111222233334444'
def numberValue = Long.valueOf(numberString)

 

Community Leader

Re: Groovy Script convert string to integer

Hi @subhaD,

 

The number which you mentioned in original post is different from the number for which you are getting error.

 

So the solution for your problem is :

 

Int datatype can only handle "2147483647" in groovy if you pass "2147483648" then it will give you the NumberFormatException Error

 

You can use Long dataType which can handle "9223372036854775807" but if you think at some point of time your number will be bigger than this then you must use BigInteger which can handle "9223372036854775807"

 

def latest = "4519021761189580"

def number = latest.toLong()

log.info number

Capture.PNG

 

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

 

Thanks and Regards,

Himanshu Tayal

Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
Watch a Video Interview
Top Kudoed Authors
Are You In?

Meet the Community Leaders!

SmartBear Connect is an excellent opportunity for training, in-person support, and networking.

GET YOUR TICKET WITH $100 OFF