Format Number as Currency

Just had the need to format some numbers into different currencies the other day so I made a simple, generic function to do the trick.

Example:

var dollars:String = "$" + currencyFormat(343769.5);
var euro:String = "€ " + currencyFormat(150, 2, ".", ",")
var nok:String = currencyFormat(10500, 2, ".", ",") + ",-"
 
trace(dollars) // $343,769.50
trace(euro) // €150,00
trace(nok) // 10.500 ,-

source:

function currencyFormat (number:Number, numDecimals:int = 2, separatorSymbol:String = ",", decimalSymbol:String = "."):String
{
	var multiplier:int = Math.pow(10, numDecimals);
	number = Math.round(number*multiplier)/multiplier;
	number = round(number, numDecimals);
	var string:String; = number.toString();
 
	var split:Array = string.split(".");
	var pre:String = split[0];
	var post:String = split[1] == null ? "" : split[1];
 
	// Format Number
 
	var _pre:String = pre.slice();
	pre = "";
 
	var count:int = 0;
	for (var i:int = _pre.length; i >= 0; i--)
	{
		var cypher:String = _pre.charAt(i);
 
		if (count % 3 == 0 && count > 0 && i > 0)
		{
			cypher = separatorSymbol + cypher;
		}
 
		pre = cypher + pre;
		count ++;
	}
 
	// Format decimals
 
	var numMissingDecimals:int = numDecimals - post.length;
	for (var i:int = 0; i < numMissingDecimals; i++)
	{
		post += "0";
	}
 
	// Join the strings
 
	string = pre;
	if (numDecimals > 0)
	{
		string += decimalSymbol + post;
	}
 
	return string;
}

Do with it as you wish!