18 CommentsMarch 26 | 2009
Here’s a little class I created to convert any number into currency format, which allows specification of decimal places and currency symbol.
Download the Currency Formatter AS3 class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | /* Use: * * import agi.utils.Format * * var _format:Format = new Format(); * * trace out: * trace(_format.currency(32783.2397,2,$)); //$32,783.24 * trace(_format.currency(32783.2397,3,$)); //$32,783.240 * trace(_format.currency(32783.2397,2,"")); //32,783.24 * */ package agi.utils { public class Format { //-----------CONSTRUCTOR-----------// public function Format() { } //-----------PRIVATE METHODS-----------// public function currency(num:Number,decimalPlace:Number=2,currency:String="$"):String { //assigns true boolean value to neg in number less than 0 var neg:Boolean = (num < 0); //make the number positive for easy conversion num = Math.abs(num) var roundedAmount:String = String(num.toFixed(decimalPlace)); //split string into array for dollars and cents var amountArray:Array = roundedAmount.split("."); var dollars:String = amountArray[0] var cents:String = amountArray[1] //create dollar amount var dollarFinal:String = "" var i:int = 0 for (i; i < dollars.length; i++) { if (i > 0 && (i % 3 == 0 )) { dollarFinal = "," + dollarFinal; } dollarFinal = dollars.substr( -i -1, 1) + dollarFinal; } //create Cents amount and zeros if necessary var centsFinal:String = String(cents); var missingZeros:int = decimalPlace - centsFinal.length; if (centsFinal.length < decimalPlace) { for (var j:int = 0; j < missingZeros; j++) { centsFinal += "0"; } } var finalString:String = "" if (neg) { finalString = "-"+currency + dollarFinal } else { finalString = currency + dollarFinal } if(decimalPlace > 0) { finalString += "." + centsFinal; } return finalString; } } } |












thak you. you saved me some time :)
Nice one. Added the following to only show decimals if they need to be shown.
if(decimalPlace > 0&& uint(centsFinal)>0
{
finalString += “.” + centsFinal;
}
yeah, i changed the last conditional statement to use only the decimal places if rather than both decimal and if cents actually exists as some people might want to show .00 in the price for some reason.
if(decimalPlace > 0)
{
finalString += “.” + centsFinal;
}
Awesome code! Thanks for sharing!
Thanks for sharing, big help.
I did notice an issue though when trying to display cents if the Number came in as an even dollar. If this were the case amountArray[1] would become “undefined” as there is no “.” to delimit with. I changed this line:
var centsFinal:String = String(amountArray[1]);
to:
var centsFinal:String;
if(amountArray[1] == undefined)
{
centsFinal = “0″;
}
else
{
centsFinal = String(amountArray[1]);
}
and it worked out fine. This was after I spent some time fooling around with this line:
var cents:Number = amountArray[1]
only to realize that variable is never used. :-P
Thanks for finding the bug. I have updated the class slightly to use toFixed instead of the maths.pow function and simplified it abit.
Should work ok now, if not, ill be in a lot of trouble at work… :(
thank you!!!
Thanks, works great. saved me some time
Thank you so much mate, saved me at least couple of hours!
A nice utility. Some countries use a postfix rather than a prefix for the currency symbol though, and some use a comma instead of a dot for seperating the units. You could have easily added support for these. It’s not very difficult to do though so can probably be left as an excercise for the reader.
@Tomas – Thanks for pointing some additional features the class could use.
I originally made the class for a project i was working on at work that required to work with only Australian currency so the additional features were’nt really necessary at the time. If i have some time up my sleeves, i’ll attempt to add some of these in and possibly clean up the code a little further.
Thanks for it! It’s a really nice utility class, a time-saver!
Keep up with the good work!
Thank you. You also saved me some time. You would think that this would be a part of the framework but o well. Thank you for taking the time to modify and share.
BTW – Tomas…You could’ve easily taken time to update the class and post it yourself …
Nice effort, saved my time.
Thanks man
thanks a lot! Saved me some time as well
too much code.
[...] 1.) We import everything we need, that includes Chris Agiasotis’ currency formatter. [...]
Pingback