# how to add leading zero to a string or integer using  groovy script

**URL:** <https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116>\
**Category:** ReadyAPI\
**Tags:** readyapi, api, migrated\
**Created:** [September 21, 2018, 4:26pm UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116 "2018-09-21T16:26:25Z")\
**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:** [September 21, 2018, 4:26pm UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116/1 "2018-09-21T16:26:25Z")

</div>

> Originally posted by **JayKondra** on September 21, 2018

how to add leading zero to a **string** or **integer** using groovy script

a = 12345

Result should be 0000012345 (Len = 10)

---

<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:** [September 23, 2018, 11:16am UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116/2 "2018-09-23T11:16:18Z")

</div>

> Originally posted by **richie** on September 23, 2018

Hi&nbsp;  
&nbsp;  
I’m not a groovy scripter - i’d like to be, but what i need is a absolutely no initial coding skills groovy course, which doesnt exist - so I tend to just steal snippets of what other people have given me and try and expand those with help - but isn’t what you want just a simple concatenation?  
&nbsp;  
Just considering how datatypes work - you wouldn’t be able to maintain any leading zeros on an integer I wouldn’t have thought cos the zero’s would get dropped as 0000012345 = 12345 if numeric - so the values would have to be treated as a string.  
&nbsp;  
my ridiculously poor coding skills have come up with the following:  
&nbsp;  
a = “12345”;  
b = “00000”;

log.info(b+a);  
When I executed the above - the response was as follows:&nbsp;&nbsp;  
&nbsp;  
Sun Sep 23 12:12:21 BST 2018: INFO: 0000012345  
&nbsp;  
Is this what you wanted? or have I missed something essential again?  
&nbsp;  
Cheers,  
&nbsp;  
richie

---

<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:** [September 23, 2018, 4:59pm UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116/3 "2018-09-23T16:59:34Z")

</div>

> Originally posted by **Lucian** on September 23, 2018

hey,

check at \</t/zero-pad-string-in-groovy-script/682\>

---

<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:** [September 24, 2018, 6:54am UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116/4 "2018-09-24T06:54:31Z")

</div>

> Originally posted by **HimanshuTayal** on September 23, 2018

Hi JayKondra

The Solution Lucian has provided is the quickest and simple but it you want to do an elaborated solution, then you can try below code :

```auto
def a = 1234
def stringLen = 10
def strLen = a.toString().size()
expectedLen = stringLen.toInteger() - strLen.toInteger()
for(i = 0; i<=expectedLen-1;i++)
{
	a = "0" + a 
}

log.info a
log.info a.length()

```

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:** [September 24, 2018, 10:18am UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116/5 "2018-09-24T10:18:59Z")

</div>

> Originally posted by **Radford** on September 24, 2018

If you have an integer value, the Sting.format method linked by Lucian is ideal, but regarding the other half of your question of padding strings…

Groovy [strings](http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/StringGroovyMethods.html) have the several padding methods included:

- padRight
- padLeft
- center

So in your example, if the variable “a” was a string, you could do the following:

```auto
def a = "12345"
def zeroPaddedString = a.padLeft(10, "0")
log.info(zeroPaddedString)

```

The following link explains the padding functions in more details:

[http://mrhaki.blogspot.com/2009/09/groovy-goodness-padding-strings.html](http://mrhaki.blogspot.com/2009/09/groovy-goodness-padding-strings.html)

---

<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:11pm UTC](https://community.smartbear.com/t/how-to-add-leading-zero-to-a-string-or-integer-using-groovy-script/116/6 "2026-10-07T17:11:47Z")

</div>

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

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