4 Replies Latest reply on Dec 8, 2006 7:43 PM by Generave

# Farmatting a number as currency

I am calculating a sale amount for an order form. I want to place that
amount in a text box as follows: \$x,xxx but Can't figure out how this is
done. Can anyone help? Thanks in advance.

• ###### 1. Re: Farmatting a number as currency
You can try this function I wrote. I tested it for \$xxx,xxx,xxx,xxx.xx and \$x.xx

Tim

• ###### 2. Re: Farmatting a number as currency
Here is another way to write it as well. Just the first thing that came to my mind. There are probably ways to improve on this.

Good Luck!
• ###### 3. Re: Farmatting a number as currency
Generave:
This function worked well. The function written by TimSimons would always

"Generave" <webforumsuser@macromedia.com> wrote in message
news:elcjag\$77m\$1@forums.macromedia.com...
> Here is another way to write it as well. Just the first thing that came
> to my
> mind. There are probably ways to improve on this.
>
> Good Luck!
>
> function ToCurrency(d:Number) : String {
> d = Math.ceil(d*100)/100;//round up to the nearest penny
> //d is the raw number inputted without formatting, for example 12345.67
> var s:String = d.toString(10);//start by converting the number to a
> string
>
> //make sure cents are in the string and if not add them
> if(s.indexOf(".") == -1){//if no period
> s += ".00";//append zero cents
> } else if (s.indexOf(".") == s.length - 2){//if last zero missing for
> cents
> s += "0";
> };
>
> //rewrite the number with commas
> for(var i:Number = 1; -3*i-2-i > -s.length; i++){
> trace(d/(1000^i));
> s = s.substr(0,-3*i-2-i) + "," + s.substr(-3*i-2-i);
> };
> return "\$" + s;
> };
> trace(ToCurrency(123456.677));//just a test
>

• ###### 4. Re: Farmatting a number as currency
You're welcome. Please ignore this line:

trace(d/(1000^i));

I accidentally left that one in there, but it serves no purpose ;)

Have a good weekend!