Formatting Numbers in AS3 / Flex
A quick google search of this topic yields, pages and pages of people rolling their own number formatting functions.
Everyone has their own little tweaks to their function. Isn’t there a built in function to do something so trivial ?
Yes, it’s the NumberFormatter class, Why does nobody talk about how to use the NumberFormatter class that has been around since Flash 9/CS3 Era ?
I’m not sure, but if you don’t want to roll your own here is some example code to get you started.
Adobe’s website gives you instructions on how to use it in a more flex style manner, if you want a pure as3 example.
// Import the class
import mx.formatters.NumberFormatter;
function somefunction(value:Number):void
{
// Initialize the NumberFormatter Object
var fmt:NumberFormatter = new NumberFormatter();
var formattedString:String;
// Set some of the options available
fmt.precision = 2; fmt.useThousandsSeparator = true;
// Format our value and return a formatted string
formattedString = fmt.format(value);
// Do something with our newly formatted string
someobject.sometextfield.text = formattedString;
}
Works like a champ.
