The CurrencyFormatter class formats numeric values into
strings that contain currency strings and formatted numbers, according
to the conventions of a specific locale.
When you instantiate a new CurrencyFormatter object, it sets
its currency to the default currency for the given locale.
The following example shows that a CurrencyFormatter object created
using a German locale assumes that currency amounts are in Euros:
var cf:CurrencyFormatter = new CurrencyFormatter( "de-DE" );
trace(cf.format(1234567.89)); // 1.234.567,89 EUR
In most cases, do not rely on the default currency for a locale.
If the user’s default locale is not supported, then the CurrencyFormatter
class assigns a fallback locale. The fallback locale can have a
different default currency. In addition, you normally want the currency
formats to look correct to your user, even if the amounts are not
in the user’s local currency. For example, a Canadian user can want
to see a German company’s prices in Euros, but formatted in the
Canadian style.
The CurrencyFormatter.setCurrency() method specifies the exact
currency string and currency symbol to use.
The following example shows currency amounts in Euros to users
in the French part of Canada:
var cf:CurrencyFormatter = new CurrencyFormatter( "fr-CA" );
cf.setCurrency("EUR", "€");
trace(cf.format(1234567.89)); // 1.234.567,89 EUR
The setCurrency() method can also be used to reduce confusion
by setting unambiguous currency symbols. For example:
cf.setCurrency("USD","US$");
By default the format() method displays a three character ISO
4217 currency code instead of the currency symbol. ISO 4217 codes
are unambiguous and do not require localization. However many users
prefer to see currency symbols rather than ISO codes.
The CurrencyFormatter class can help you decide which symbol
a formatted currency string uses: a currency symbol, like a dollar
sign or Euro sign, or a three character ISO currency string, such
as USD or EUR. For example, an amount in Canadian dollars could
be displayed as $200 for a user in Canada. For a user in the United
States, however, it could be displayed as CAD 200. Use the method formattingWithCurrencySymbolIsSafe()
to determine whether the amount’s currency symbol would be ambiguous
or incorrect given the user’s locale settings.
The following example formats a value in Euros into a format
for the en-US locale. Depending on the user’s locale, the output
string uses either the ISO currency code or the currency symbol.
var cf:CurrencyFormatter = new CurrencyFormatter( "en-CA");
if (cf.formattingWithCurrencySymbolIsSafe("USD"))
{
trace(cf.format(1234567.89, true)); // $1,234,567.89
}
else
{
cf.setCurrency("USD", "$");
trace(cf.format(1234567.89)); // USD1,234,567.89
}