On This Page
Overview and Description
Currency formatting needs to take into consideration these following elements:
Most currencies use the same decimal and thousands separator that the numbers in the locale use, but this is not always true. In some places in Switzerland, they use the period as a decimal separator for Swiss frans (Sfr. 127.54), but then use commas as the decimal separator everywhere else (127,54).
Figure 1: Currency formatting in Regional Options
Currency Formatting in Win32
Win32 NLS APIs can help you display currency data in a way that's locale-aware. The GetCurrencyFormat function formats a number string as a currency string for a specified locale. The code sample on the following page formats a number string into the user locale's default format:
GetCurrencyFormat(LOCALE_USER_DEFAULT, // a predefined value for user locale
Execution of the previous code would give the following result on English (United States) and Danish user locales, respectively. (See Figure 2.)
Figure 2 - Currency format for English (United States) and Danish.
With this approach, the number string is formatted to a locale-specific format and, more importantly, currency symbol! But suppose you want to represent $1,230.40 (U.S. dollars) to a Danish user. By using the first approach mentioned and formatting your string with current user locale settings (Danish), your string would in fact come out as kr 1.230,40. Of course, the main issue would be that you want to format your string in the Danish format, but you don't want to change your currency symbol (and in this case lose some of the value of your money in the transaction).
Another scenario would be the use of the euro as the official currency of Belgium, Germany, Spain, France, Ireland, Italy, Luxembourg, the Netherlands, Austria, Portugal, Finland, and Greece. (This list is as of January 1, 2002.) The .NET Framework and Windows XP set the default currency symbol to the euro for these twelve countries that use the euro as their official currency. However, older versions of Windows, without any intervention, will still set the default currency symbol to the local currency for these countries or regions. To resolve the issue in the two scenarios just mentioned, you can take advantage of the
lpFormat argument of GetCurrencyFormat. This argument is a pointer to a currency-formatting structure, where you can define your own formatting model and specify the currency symbol to be used following the formatting standard of a given locale. The following code sample uses the formatting for the currently selected user locale, but with the euro as the desired currency symbol. Here is how it works.
By following the code, the result of a currency symbol formatting for the Danish locale (with kr as default currency symbol) would be: € 123,40
Currency Formatting in the .NET Framework
The .NET format strings are useful if you want to convert one of the standard .NET Framework data types to a string that represents that type in some other format. For example, if you have an integer value of 100 that you want to represent to the user as a currency value, you could easily use the ToString method and the currency-format string ("C") to produce a string of "$100.00". Computers that do not have English (United States) specified as the current culture will display whatever currency notation is used by the current culture. The original value contained in the data type is not converted, but a new string is returned that represents the resulting value. This new string cannot be used for calculation until it is converted back to a .NET base data type. The original value, however, can be calculated at any time.
In the following code example, the ToString method displays the value of 100 as a currency-formatted string in the console's output window.
int MyInt = 100;
This code displays $100.00 to the console on computers that have English (United States) as the current culture.
The CurrencySymbol property of the RegionInfo class from the System.Globalization namespace can be used to retrieve the currency symbol associated with the country or region. Also, the ISOCurrencySymbol property of RegionInfo retrieves the three-character ISO 4217 currency symbol associated with the country or region.
Regarding the euro challenge mentioned earlier, again, the euro is now the default currency symbol of twelve European countries, but only Windows XP has this currency symbol set as the default for the affected countries. On downlevel platforms (including Windows 2000), this value has to be set manually by the user. If you want to ensure that your application uses the .NET Framework's default settings and use the euro as the default currency symbol (regardless of the user settings), you must create a CultureInfo object, passing a useUserOverride parameter set to false.
The following example uses code that is similar to the previous example. It sets the current culture to "fr-FR" and displays an integer to the console formatted as currency. The user's local currency settings are used to format the currency. Next, the culture is set to "fr-FR" using the CultureInfo constructor that accepts the useUserOverride parameter set to false. The number is then formatted using the .NET Framework default settings, and the euro currency symbol is displayed.
Although most European Union countries now use the euro, there might be situations-such as for legacy reasons-where it is necessary to display both the euro and the older currencies in an application. The following code example creates a CultureInfo object for the culture "fr-FR" where the default currency is the euro. To display the currency symbol for the local currency, you must use the
NumberFormatInfo.Clone method to clone a new
NumberFormatInfo object for the CultureInfo object and replace the default currency symbol with a local currency symbol.
Currency Formatting in Web Pages
In "Retrieving the Browser Language Setting" earlier in this chapter, you saw how to retrieve the current browser locale on the client side and how to set the global locale of your context or session to this value. Once the appropriate locale has been set, you can easily format currency by using FormatCurrency, a locale-aware function. Suppose you have retrieved Bulgarian as the primary browser locale ("bg" being the default value for this locale). The following code saves the current context locale (matching the server's user locale), sets the locale to Bulgarian, formats the date in Bulgarian format, and restores the original locale.
currentLocale = GetLocale
And the output would be: 3 943,23
Obviously the scripting technology does not offer the same flexibility to manipulate currency symbols as NLS APIs do in the case of Win32 programming. However, the FormatCurrency function gives you the ability to display currency according to the user's cultural preferences, in both short and long currency formats.