Number Format – Thousand Separator in AS3
Here’s a short number format function I wrote to easily paste in your code when needed. It’s really handy for currency formatting.
The first parameter (number:*) can be a Number, int, uint or a String class instance.
The last parameter (siStyle:Boolean) specifies whether to use the International System of Units or not. SI units have points between the thousands and a comma for the seperator (123.456.789,01). Putting siStyle as false reverses that behaviour (123,456,789.01).
It’s really ugly by design since I wanted it to be a single, tiny function. There’s loads of prettier/faster code samples out there.
function numberFormat(number:*, maxDecimals:int = 2, forceDecimals:Boolean = false, siStyle:Boolean = false):String { var i:int = 0, inc:Number = Math.pow(10, maxDecimals), str:String = String(Math.round(inc * Number(number))/inc); var hasSep:Boolean = str.indexOf(".") == -1, sep:int = hasSep ? str.length : str.indexOf("."); var ret:String = (hasSep && !forceDecimals ? "" : (siStyle ? "," : ".")) + str.substr(sep+1); if (forceDecimals) for (var j:int = 0; j <= maxDecimals - (str.length - (hasSep ? sep-1 : sep)); j++) ret += "0"; while (i + 3 < (str.substr(0, 1) == "-" ? sep-1 : sep)) ret = (siStyle ? "." : ",") + str.substr(sep - (i += 3), 3) + ret; return str.substr(0, sep - i) + ret; }
Tags: AS3, currency, format, function, number, quick, separator, short, thousand
October 9th, 2009 at 15:25
I like your snippet, thank you. Only thing there’s an error which leads to one decimal place less than expected.
WRONG:
for (var j:int = 0; j < maxDecimals – (str.length – sep); j++)
RIGHT:
for (var j:int = 0; j < maxDecimals – (str.length – 1 – sep); j++)
October 9th, 2009 at 18:06
Aaah, you’re absolutely right! It’s fixed now.
To hold onto compactness I changed
to
Thanks for spotting that =)
October 20th, 2009 at 21:37
You’ve also got the comma and decimal around the wrong way.
October 21st, 2009 at 10:11
No, that’s what the siStyle property is for, you can actually choose the metric SI style (default true because of majority) or non-SI style (false) that’s in use in mostly Burma (Myanmar), Liberia, and the United States.
October 31st, 2009 at 10:33
Thanks, I was just looking for something like this. Nice code, also.
November 20th, 2009 at 13:00
I don’t see what was wrong with your original Deciaml code. When I test it your first line works – the second ads one decimal too many.
November 20th, 2009 at 13:31
Torben, I found the error you were talking about; it only happened when you passed in whole numbers.
It’s been fixed now, thanks for spotting it =)
November 24th, 2009 at 11:24
Great, thanks Flassari
December 30th, 2009 at 16:55
Hey, I added a little tweak of mine to your function. What it does is that it indents the returning string with indentUnit so that the length of the string minus decimals becomes forceLength.
This can be useful when you intend to align a vertical list of numbers so that the decimal points are in a straight line.
Note: unless you specify the forceLength parameter as a uint higher than 0, this function behaves just like the original function made by Flassari.
April 15th, 2010 at 21:44
nice functionality but I hate your formatting. It isn’t clear enough to be updated easily. You don’t need to show that you can program like a nasa guy when you give a function on the web. Most people who will use your function will be beginners…
But thank you for sharing it
April 15th, 2010 at 22:15
The formatting was done on purpose. I know it’s massively compressed and horrible to read, but it’s supposed to be small and convenient to paste into timeline or code. There were other resources which had dedicated classes which are easily changeable and commented (like for beginners) at the time I made this (and still are), but I didn’t want a whole class when I just wanted something quick and small to insert into the timeline.
I also don’t recommend this code for mass number processing since it is not at all optimized for speed, only compactness.
March 29th, 2011 at 01:06
Hi Flassari!
You saved me a lot of time!
Many many thanks from Brasil!
May 6th, 2011 at 11:55
great function, thanks alot
October 18th, 2011 at 19:26
works perfectly, thanks.
November 16th, 2011 at 23:28
This is exactly what i was looking for. Regards. MX
December 9th, 2011 at 16:21
Great stuff. Thanks for saving me some time when I had none :)
December 14th, 2011 at 14:43
Hey! Thanks alot for sharing this. It helped me big time!