So I have a dynamic text field on stage (stage_clickCounter) where every time I click on said button the variable clickCounter goes up by 1.
I'm using this piece of code to display the numbers on screen:
stage_clickCounter.text = numberOfClicks.toString();
The numbers show up fine and all, but once I get to 1,000 clicks it shows up without the comma
1000//shows this on dynamic text field
1,000//would much rather prefer this
How can I do this? I have numbers and punctuations embedded into the font already.
For positive integers:
function addComma(num:uint):String{
if(num == 0) {
return "0";
}
var str:String="";
while(num>0){
var tmp:uint=num%1000;
str=(num>999?","+(tmp<100?(tmp<10?"00":"0"):""):"")+tmp+str;
num=num/1000;
}
return str;
}
Usage:
trace(addComma(1000999333)); //output:1,000,999,333
Edit:
For negative and positive integers:
function addComma(num:int):String{
if(num == 0) {
return "0";
}
var str:String="";
var sign:String=num>0?"":"-";
num=Math.abs(num);
while(num>0){
var tmp:int=num%1000;
str=(num>999?","+(tmp<100?(tmp<10?"00":"0"):""):"")+tmp+str;
num=num/1000;
}
return sign+str;
}