My name is LordAlex Leon, back in year 1998, I came across a nice little plug-in called Flash, little I knew this would completely change my life.
Since that moment I primarily make a living at consulting and helping companies/startups deliver intelligent rich internet content and powerful applications based on the Adobe Flash Platform.
I founded my own consulting company LordAlex Works and enjoy speaking at many international conferences and events whenever I can.
MoneyFormat Class in ActionScript 2.0
I've been asked for an AS2 version of my original MoneyFormat Class, by quite some people, so I decided to do quick conversion, and post it here. I'll be working on this to extend its functionality very soon.
Enjoy.
+LA
/**
* MoneyFormat, Version 1.
* Transforms regular numbers into Currency Format.
* Makes use of the String Class
* Updates at: www.lordalex.org - reach@lordalex.org
*
* @author: LordAlex Leon
* @version: 1.0.0
**/
class MoneyFormat extends String {
private var _sign:String;
private var _sep:String;
private var _dec:String;
private var _decimals:Boolean;
/**
* MoneyFormat Constructor
* @param sign_param [String] The currency sign. ie: "$"
* @param sep_param [String] The unit delimiter. ie: ","
* @param dec_param [String] The decimal delimiter. ie: "."
* @param decimals_param [Boolean] Option to display last 2 decimal places. ie: true or false
**/
function MoneyFormat(sign_param:String, sep_param:String, dec_param:String, decimals_param:Boolean) {
(sign_param == undefined) ? _sign="$" : _sign=sign_param;
(sep_param == undefined) ? _sep="," : _sep=sep_param;
(dec_param == undefined) ? _dec="." : _dec=dec_param;
(decimals_param == undefined) ? _decimals=true : _decimals=decimals_param;
}
/**
* toMoney method
* Takes one paramter and transforms the value into money format
* @param amount_param [String] Takes in a String value.
**/
public function toMoney(amount_param:String):String {
if ((amount_param == "") || (amount_param == undefined)) {
return "";
} else {
var _myDot:Number;
var _myString:String;
var myvalue:String;
var cents:String;
var myLength:Number;
var divide:Number;
var dollars:String;
(amount_param) ? _myString=amount_param.toString() : _myString="";
_myDot = _myString.indexOf(".");
if (_myDot<=0) {
myvalue = _myString;
cents = "00";
} else {
myvalue = _myString.substr(0, _myDot);
cents = _myString.substr(_myDot+1, _myString.length);
}
if (myvalue.length>0) {
myLength = myvalue.length;
divide = myLength/3;
if ((myLength%3) == 0) {
divide = (myLength/3)-1;
}
for (var i = 1; i<=divide; i++) {
myvalue = myvalue.substr(0, (myLength-(3*i)-(i-1)))+_sep+myvalue.substr((myLength-(3*i)-(i-1)), (3*i)+(i-1));
myLength = myvalue.length;
}
dollars = myvalue;
}
if (cents.length>2) {
cents = cents.substr(0, 2)+_dec+cents.substr(2, cents.length);
} else if (cents.length == 1) {
cents = cents+"0";
}
if (_decimals) {
return _sign+dollars+_dec+cents;
} else {
return _sign+dollars;
}
}
}
}
posted by LordAlex Leon @ 2:05:00 AM

8 Comments:
Why does toMoney() require a string?
Can I use toMoney("Eight dollar and forty nine cents");? Will it return $8.49? Cool if does!
Thanks
Umm... Anonymous, well. It would apear that 34,15 and 34.15 are not the same. depending on your country, and your localization settings. should I continue to explain, or is that enough to make sense of the String usage??
cheers, julian
Thank you Julian, that is actually one the reasons why I take a string instead of a number. In USA we use the "," to separate the numbers and "." for decimals, where in other countries in the complete opposite.
Thanks for feedback.
+LA.
LA, can you repost a link to download the .as file. thanks.
Sure I can I will place one later tonight.
Cheers!
+LA
Hello Friends,
Here is the link to the as file:
http://www.lordalex.org/AS/MoneyFormat.as
Enjoy!
+LA
Hi LA!
I am probablly the biggest idiot ever, but I can't figure out how to use your class. I have placed and imported the class file properly, etc so this has to do with my actual usage of your class in my script.
What I am trying to do is import SQL data through ASP.NET into Flash via XML and then set the XML node values to the text property of certain dynamic text boxes. This part I have no problem with; however, when I try to use your class, I get "undefined" for the value of the tb instead of the real value (which I do get if I don't use your class).
Here's an example of what I am doing without your class:
cost_txt.text = cost[0];
With your class:
cost_txt.text = toMoney(cost[0]);
Could you please point out what I am doing wrong here? Any help would be greatly appreciated!!!
Matt
Great class, Alex. I'm not sure if this was a bug, but I was getting an error with numbers with decimal places until I changed this line:
cents = cents.substr(0, 2)+_dec+cents.substr(2, cents.length);
to this:
cents = cents.substr(0, 2)
Post a Comment
Links to this post:
Create a Link
<< Return to Home Page