# Trouble adding commas to string

**URL:** <https://forum.kirupa.com/t/trouble-adding-commas-to-string/186081>\
**Category:** flash\
**Created:** [April 18, 2006, 9:52pm UTC](https://forum.kirupa.com/t/trouble-adding-commas-to-string/186081 "2006-04-18T21:52:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![james704](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@james704](https://forum.kirupa.com/u/james704)\
**Post date:** [April 18, 2006, 9:52pm UTC](https://forum.kirupa.com/t/trouble-adding-commas-to-string/186081/1 "2006-04-18T21:52:55Z")

</div>

I’m using code I found on this site to add commas to a large number (so that a number like 1000000 appears as 1,000,000). Below is the actionscript I’m using.

When I define the variable debt as any number, the script works perfectly. But if I define debt as a variable passed to the movie from a PHP file, then the script puts the commas in the wrong places (i.e., 10,000,00). Can anyone explain to me why this would happen, and how to fix it?

var input = debt;  
var inputString = input.toString();  
var finalString = “$”;  
var count = 0;  
var tempString = “”;  
for (var i = inputString.length-1; i\>=0; i–) {  
count++;  
tempString += inputString.charAt(i);  
if ((count%3 == 0) && (i - 1 \>= 0)) {  
tempString += “,”;  
}  
}  
for (var k = tempString.length; k\>=0; k–) {  
finalString += tempString.charAt(k);  
}  
return(finalString);

---

<div class="post-metadata">

**Author:** ![joran420](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joran420/32/2329_2.png) [@joran420](https://forum.kirupa.com/u/joran420)\
**Post date:** [April 18, 2006, 11:46pm UTC](https://forum.kirupa.com/t/trouble-adding-commas-to-string/186081/2 "2006-04-18T23:46:07Z")

</div>

> [@james704](#):
>
> I’m using code I found on this site to add commas to a large number (so that a number like 1000000 appears as 1,000,000). Below is the actionscript I’m using.
> 
> When I define the variable debt as any number, the script works perfectly. But if I define debt as a variable passed to the movie from a PHP file, then the script puts the commas in the wrong places (i.e., 10,000,00). Can anyone explain to me why this would happen, and how to fix it?
> 
> var input = debt;  
> var inputString = input.toString();  
> var finalString = “$”;  
> var count = 0;  
> var tempString = “”;  
> for (var i = inputString.length-1; i\>=0; i–) {  
> count++;  
> tempString += inputString.charAt(i);  
> if ((count%3 == 0) && (i - 1 \>= 0)) {  
> tempString += “,”;  
> }  
> }  
> for (var k = tempString.length; k\>=0; k–) {  
> finalString += tempString.charAt(k);  
> }  
> return(finalString);

I THINK you have to use the escape char  
tempstring = “,”

---

<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:** [April 18, 2006, 11:54pm UTC](https://forum.kirupa.com/t/trouble-adding-commas-to-string/186081/3 "2006-04-18T23:54:31Z")

</div>

> [@joran420](#):
>
> I THINK you have to use the escape char  
> tempstring = “,”

No that’s not it. It might be that there is a space in the string you load from PHP 🙂
