Formatting Numbers in Flash for Commas

Here is how to use actionscript to format a number in Flash with commas per thousands.

my_number = “12587654325”;

// array in sections of 3 to separate with commas
trace(my_number);
var dollar_array:Array = new Array();
var start:Number;
var end:Number = my_number.length;
trace(my_number.length);
while (end > 0) {
trace(end);
start = Math.max(end – 3, 0);
dollar_array.unshift(my_number.slice(start, end));
end = start;
}

// assign a comma delimited value from dollar_array
my_number = dollar_array.join(“,”);
trace(my_number);

You may also like...

1 Response

  1. Winkyboy says:

    Thanks. With a little conversion of extended characters to regular ones (like the fancy “” to regular quotes), this works nicely. Providing this form allows all the characters, here’s this code converted into a function:

    private function commify(my_number:Number):String {
    // array in sections of 3 to separate with commas
    var strNumber:String = my_number.toString();
    var dollar_array:Array = new Array();
    var start:Number;
    var end:Number = strNumber.length;

    while (end > 0) {
    start = Math.max(end – 3, 0);
    dollar_array.unshift(strNumber.slice(start, end));
    end = start;
    }

    // assign a comma delimited value from dollar_array
    return dollar_array.join(“,”);
    }

Leave a Reply