Recipe 4.6. Formatting Currency Amounts


Problem

You want to format a number as currency, such as dollars.

Solution

Use the NumberFormat.currencyFormat( ) method.

Discussion

Unlike some other languages, such as ColdFusion, ActionScript does not have a built-in function for formatting numbers as currency amounts. However, the custom NumberFormat class includes a currencyFormat( ) method that takes care of basic currency formatting for you.

The currencyFormat( ) method requires at least one parameter; the number you want to format as currency. The following example illustrates the simplest use of currencyFormat( ):

var styler:NumberFormat = new NumberFormat(  ); trace(styler.currencyFormat(123456));

Assuming that the preceding code is run on a U.S. English computer, the output is as follows:

$123,456.00

As with the format( ) method of the NumberFormat class discussed in Recipes 4.4 and 4.5, the currencyFormat( ) method uses automatic localization detection settings. Therefore, if the preceding code is run on a computer in Spain running a Spanish operating system, the output is as follows:

123.456,00

However, the Locale class (which is responsible for determining the locale from where the application is being run) may not be able to correctly detect the locale. Furthermore, you may simply want to override automatic localization so you get a consistent value regardless of where the application is run. There are several ways you can override the automatic localization detection; the same ways that you can override the localization settings when using the format( ) method:

  • Use a Locale object as the second parameter when calling currencyFormat( ).

  • Assign global values to the Locale.slanguage and/or Locale.svariant properties.

  • Use a symbols object as the second parameter when calling currencyFormat( ).

The symbols object for currencyFormat( ) is slightly more complex than the symbols object for the format( ) object. If you use a symbols object with currencyFormat( ), you should include the following four properties: group, decimal, currency, and before. The group and decimal properties act just as with the format( ) method. The currency property should have a value of the currency symbol you want to use. The before property is a Boolean value in which true means the currency symbol should appear before the numbers, and false means the symbol should appear after the numbers.

The following is an example of different ways of overriding the localization settings with currencyFormat( ):

var styler:NumberFormat = new NumberFormat(  ); trace(styler.currencyFormat(123456)); Locale.slanguage = "nl"; trace(styler.currencyFormat(123456)); trace(styler.currencyFormat(123456, new Locale("sv"))); trace(styler.currencyFormat(123456, {group: ",", decimal: ".", currency: "@", before: false}));

The preceding code outputs the following results:

$123,456.00 123.456,00 123,456.00kr 123,456.00@

See Also

Recipes 4.3, 4.5, and Appendix A for creating special characters, including the Euro (), Yen (¥), and British pound ( £) symbols. To align currency amounts in text fields, set the field's format to right justification using the TextFormat .align property.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net