CGI Interactions
A blog about interactive communications for marketers, designers and developers

Formatting Numbers With Commas in AS3

May 15th, 2009 . by nixon

1000 or 1,000? The number is pretty easy to decipher regardless, so get off my back about the commas, right? How about 1000000 or 10000000? This is where the sweet, sweet comma can really come in handy.

Recently we built an opportunity assessment calculator for a client. The tool allowed our client’s sales force to work with retail managers and determine how they could increase profitability with their e-commerce enabled website. The tool allowed sales to show, in dollars and cents, what small improvements and changes could mean to their bottom line. Awesome for the client, kinda strenuous for the developer (moi) because numbers need to constantly change from a bare bones format that make AS3’s math functions happy, to a beautiful looking piece of art that might appear on a bank statement.

Adding in a dollar sign is easy, but how to add commas!? Being that AS3 offers virtually no support for number formatting I found myself cruising the private sector; reading one code monkeys opinion after another. While I found plenty of functions, it seemed like they all made assumptions on what the expected input would be. No numbers larger than 9999, no negative numbers, no decimal points, etc. I finally tossed on the ol’ headphones and cranked out my own addCommas function. It accepts a real number as an input and spits back the same number as a string with commas.

Have fun, make sure to have her home by ten:

function addCommas(number:Number):String {
var negNum:String = “”;
if(number<0){
negNum = “-”;
number = Math.abs(number);
}
var num:String = String(number);
var results:Array = num.split(/\./);
num=results[0];

if (num.length>3) {
var mod:Number = num.length%3;
var output:String = num.substr(0, mod);
for (var i:Number = mod; i<num.length; i += 3) {
output += ((mod == 0 && i == 0) ? “” : “,”)+num.substr(i, 3);
}
if(results.length>1){
if(results[1].length == 1){
return negNum+output+”.”+results[1]+”0″;

}else{
return negNum+output+”.”+results[1];
}
}else{
return negNum+output;
}
}

if(results.length>1){
if(results[1].length == 1){
return negNum+num+”.”+results[1]+”0″;

}else{
return negNum+num+”.”+results[1];
}
}else{
return negNum+num;
}}

Last 5 posts by nixon

Share:
  • del.icio.us
  • Digg
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Mixx
  • Tumblr
  • Add to favorites
  • Posterous
  • RSS
  • Yahoo! Bookmarks

Posted in: developer station

Tagged Under:
, , , , , , , , ,

6 Comments »

6 Responses to “Formatting Numbers With Commas in AS3”

  1. comment number 1 by: Wyeth

    Hi there,
    Thanks for your script. It helped me. I had a problem though. When the number got up into higher digits, the commas were being placed in the wrong places. I tried a few things and then ended up replacing

    var mod:Number = num.length/3;

    with a switch statement:

    switch(num.length){
    case 4:
    mod = 1;
    break;

    case 5:
    mod = 2;
    break;

    case 6:
    mod = 3;
    break;

    case 7:
    mod = 1;
    break;

    case 8:
    mod = 2;
    break;

    case 9:
    mod = 3;
    break;
    }

    It worked perfectly after that.

    Thanks again,

    Best,

    -w

  2. comment number 2 by: Nixon

    hey there Wyeth,
    I’ve cranked some pretty big numbers through the function without a problem…

    I think the issue is that line you break out:
    var mod:Number = num.length/3;

    In actuality should read:
    var mod:Number = num.length%3;

    The percent sign being the modulus math operation. The modulus operator is doing the same thing as your switch statement in that, 4%3 = 1, 5%3=2 etc etc. Unfortunately the switch statement you’ve outlined won’t work on numbers with more than 9 digits.

    I would replace the switch with this:
    var mod:Number = num.length%3;

    I think the problem is from wordpress really butchering the code format… maybe I can attach a file for download…

  3. comment number 3 by: Jack

    Thanks for posting the code. Works great and saved me a ton of time.

    -Jack

  4. comment number 4 by: Scott

    I took a different approach — should work for any sized number. I didn’t need the negative detection, you should be able to add that to this easily if you need it.

    public function addCommas(number:Number):String
    {
    var numberString:String = String(number);
    var newString:String = “”;
    var col:int = 0;
    for (var index:int = numberString.length-1; index >= 0; index–)
    {
    col++;
    newString = numberString.substr(index, 1) + newString;
    if (col == 3 && index != 0)
    {
    newString = “,” + newString;
    col = 0;
    }

    }
    return newString;
    }

  5. comment number 5 by: nixon

    While your solution will work for any sized positive number it won’t handle numbers with a decimal point.

    For example 5.25 would be formatted 5,.25

    Our two functions aren’t behaving that much differently, personally, I find the flexibility to work with negative numbers and decimal points is worth the extra few lines of code because I can reuse the same function anytime I need formatted numbers and don’t have to revisit it when a special case comes along.

  6. comment number 6 by: Andrew

    As with jack – this came in very handy and works as expected; thanks for sharing it.

Leave a Reply

Name

Mail (never published)

Website

Note: Because we value your thoughtful opinions, we encourage you to add a comment to this discussion. We reserve the right edit your comments for clarity or to keep out questionable matters, however, and we may even delete off-topic comments.



home     |     about     |     disclaimer     |     cgi interactive
2009 - CGI Interactive - 76 Otis Street - Westboro, MA 01581 - 508.898.2500