# Groovy Script convert string to integer

**URL:** <https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219>\
**Category:** ReadyAPI\
**Tags:** readyapi, api, migrated\
**Created:** [October 10, 2018, 8:47pm UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219 "2018-10-10T20:47:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 10, 2018, 8:47pm UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219/1 "2018-10-10T20:47:47Z")

</div>

> Originally posted by **subhaD** on October 10, 2018

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

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 11, 2018, 8:36am UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219/2 "2018-10-11T08:36:30Z")

</div>

> Originally posted by **HimanshuTayal** on October 11, 2018

Hi subhaD,

It’s working fine for me.

 ![Capture.PNG](https://canada1.discourse-cdn.com/flex049/uploads/smartbear_prod/original/1X/9d78fe161d02d8c9cf145439e3730015fcb2c233.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” 🙂

Thanks and Regards,

Himanshu Tayal

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 11, 2018, 1:56pm UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219/3 "2018-10-11T13:56:09Z")

</div>

> Originally posted by **subhaD** on October 11, 2018

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

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 11, 2018, 3:45pm UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219/4 "2018-10-11T15:45:00Z")

</div>

> Originally posted by **Radford** on October 11, 2018

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](http://docs.groovy-lang.org/docs/next/html/documentation/core-syntax.html#_numbers)

You will need to use the “Long” data type instead:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 12, 2018, 4:32am UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219/5 "2018-10-12T04:32:17Z")

</div>

> Originally posted by **HimanshuTayal** on October 11, 2018

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 “214748364 **8** ” 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”

```auto
def latest = "4519021761189580"

def number = latest.toLong()

log.info number

```

 ![Capture.PNG](https://canada1.discourse-cdn.com/flex049/uploads/smartbear_prod/original/1X/3d771d9e0b7f7c18b7a0fd7e28c75d8b9a294cb7.png)

Click “Accept as Solution” if my answer has helped, and remember to give “kudos” 🙂

Thanks and Regards,

Himanshu Tayal

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 7, 2026, 5:13pm UTC](https://community.smartbear.com/t/groovy-script-convert-string-to-integer/219/6 "2026-10-07T17:13:44Z")

</div>

Closed on October 7, 2026, 5:13pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
