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:

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑