• flickr
  • linkedin
  • twitter
  • rss
adding commas to big number counts in AS3

adding commas to big number counts in AS3

Just a handy snippet I pinched:

function addCommaInNumber (number : Number) : String
{
var integer : String = "" ;
var fraction : String = "" ;
var string : String = number.toString();
var array : Array = string.split(".");

var regex : RegExp = /(\d+)(\d{3})/;

integer = array[0];

while( regex.test(integer) )
{
integer = integer.replace(regex,'$1' + ',' + '$2');
}

if (array[1])
{ fraction = integer.length > 0 ? '.' + array[1] : ''; }

return integer + fraction;
}

function removeCommaInNumber (string : String) : Number
{
var regex : RegExp = /,/g;
return Number (string.replace(regex,"")) ;
}
Tech Reference:

Larry's a 30-something chap interested in travel, being a dork and changing the world via less boring training.

Leave a reply