# Padding 0 to toString

**URL:** https://forum.kirupa.com/t/padding-0-to-tostring/265972
**Category:** Uncategorized
**Created:** [July 15, 2008, 3:44am UTC](https://forum.kirupa.com/t/padding-0-to-tostring/265972 "2008-07-15T03:44:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tensaix2j](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tensaix2j](https://forum.kirupa.com/u/tensaix2j)
#### Post date: [July 15, 2008, 3:44am UTC](https://forum.kirupa.com/t/padding-0-to-tostring/265972/1 "2008-07-15T03:44:13Z")

</div>

I would like to know in AS3.0, how do i

pad 0 in front of the integer upon converting to string  
eg:

1 --\> 0001  
11 --\> 0011

so that i ll have consistent number of digits…

---

<div class="post-metadata">

### Author: ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)
#### Post date: [July 15, 2008, 4:45am UTC](https://forum.kirupa.com/t/padding-0-to-tostring/265972/2 "2008-07-15T04:45:57Z")

</div>

```auto

function fullLength(val:String, targLength:int = 4):String
{
	var retS:String = val;
	
	if (val.length < targLength)
	{
		for (var z:int = 0; z < targLength - val.length; z++)
		{
			retS = "0" + retS;
		}
	}
	
	return retS;
}

trace(fullLength("1")); //returns 0001
trace(fullLength("621")); //returns 0621

```

That should do the trick. :thumb:

_ **edit:** _ I added it as a parameter that defaults to 4. If you want a different length, change the default, or pass it like this:

```auto

trace(fullLength(myString, targetLength));

```

---

<div class="post-metadata">

### Author: ![tensaix2j](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@tensaix2j](https://forum.kirupa.com/u/tensaix2j)
#### Post date: [July 15, 2008, 5:22am UTC](https://forum.kirupa.com/t/padding-0-to-tostring/265972/3 "2008-07-15T05:22:35Z")

</div>

i thought there should be a built in function for this …  
but thanks anyway. 🙂

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [July 15, 2008, 5:50am UTC](https://forum.kirupa.com/t/padding-0-to-tostring/265972/4 "2008-07-15T05:50:56Z")

</div>

You could also do:

```auto
function fullLength(val:String, targLength:int = 4):String {
	return val.length < targLength ? new Array(targLength - val.length + 1).join("0") + val : val;
}

trace(fullLength("1")); //returns 0001
trace(fullLength("621")); //returns 0621 

```

---

<div class="post-metadata">

### Author: ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)
#### Post date: [July 15, 2008, 12:29pm UTC](https://forum.kirupa.com/t/padding-0-to-tostring/265972/5 "2008-07-15T12:29:44Z")

</div>

Or just have it untyped until you pad the 0 and then cast it.
