Skip to main content
Participating Frequently
July 8, 2011
Question

help!! currency format

  • July 8, 2011
  • 2 replies
  • 869 views

i use float to store my item price, i wan to display it in textbox with currency format like ($1.50), what should i do?

This topic has been closed for replies.

2 replies

Inspiring
July 8, 2011

In addition to relaxatraja's suggestion to use toFixed(), here is a full currency formatter with American number conventions:

var number:Number = 13458900.3;
var formatted:String = number.toFixed(2);
formatted = "$" + formatted.replace(formatted.indexOf(".") > -1 ? /(?<=\d)(?=(\d\d\d)+(?!\d)(?:\.\d*))/g : /(?<=\d)(?=(\d\d\d)+(?!\d))/g, ",").replace(/,{2,}/g, ",");
trace(formatted);

relaxatraja
Inspiring
July 8, 2011

Good! Itz more specific with the function tofixed();

relaxatraja
Inspiring
July 8, 2011

If you dont want the conversion then just use

txtDisplay.text="$"+floatValue;
keithlyeAuthor
Participating Frequently
July 8, 2011

but my boss wan me to show 2 decimals point always, i try use currencyformat but it like not supported in flash cs4...

Ned Murphy
Legend
July 8, 2011

If you want true currency format then you need to deal with String operations to analyze the value that is being displayed and adjust it with "$", "0", and "," characters as needed.

For instance, you can analyze the string to see if it has a ddecimal point, and if not, you know you need to tack on ".00", and if it does have a decimal you check to see what the index of that decimal is relative to the last character to determine if you need to add a "0".  Then you can split off the leading part and place commas in where they belong.  And then, as a final step you sew the parts back together and tack on a "$"

You might be able to find something searching Google that will show you how to manage it if you use search terms like "AS3 currency format"

While I don't have it myself, I have read that CS5 has a CurrencyFormat class, so maybe it's time to sway your boss into upgrading your tools.