Formatting Numbers With Commas in AS3
May 15th, 2009 . by nixon1000 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
- Accessible htaccess files - July 6th, 2009
- MDM Zinc vs. Adobe AIR - July 1st, 2009
- How to use simpleXML in PHP (guestbook example) - June 23rd, 2009
AS3, comma, decimal, flash, format, how to, negative, number, programming, tutorial
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
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…
Thanks for posting the code. Works great and saved me a ton of time.
-Jack
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;
}
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.
As with jack – this came in very handy and works as expected; thanks for sharing it.