Currency Formatting

A locale-aware and culture-sensitive representation of currency needs to take into consideration the following elements:

Currency symbol. This can be a predefined symbol like the European euro "" or a combination of letters like the use of "lei" for the Romanian's Lei.

Currency symbol placement. It can be placed either before or after the digits.

Negative-amount display. There are several ways to display negative amounts. (See Table 4-5.)

Most currencies use the same decimal and thousands separators that the numbers in the locale use, but this is not always true. In Estonia, they use the period as a decimal separator for Estonian Kroons (127.54 kr), but then use commas as the decimal separator for everything else (127,54).

Table 4-5 Currency conventions in various locales for negative amounts.

Currency Convention

Locale

Result

The negative sign before both the currency symbol and number

U.K.
France

-£127.54
-127,54

The negative sign before the number but behind the currency symbol

Denmark

kr-127,54

The negative sign after the number

Netherlands

127,54 -

The use of parentheses

U.S.

($127.54)

The user can define the preferred currency symbol as well as the positive and negative currency formats by making selections from the Currency tab of the Customize Regional Options property sheet, within the Regional And Language Options property sheet. (See Figure 4-18.)

Important


All techniques described in this section about handling currency formats in Win32, Web pages, and the .NET Framework only deal with the actual representation of data. The system does not offer a conversion mechanism between different monetary units. It's up to developers to implement that extra step. For example, if you decide to format $1,000 into Dutch (Belgium) standards, the system will return to you 1.000 , even though $1 is not equivalent to 1 .

figure 4-18 currency formatting for china (prc).

Figure 4-18 - Currency formatting for China (PRC).

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    NULL,               // operation option    TEXT("123.40"),     // input number (see MSDN for accepted chars)    NULL,               // formatting specifications    g_szTemp,           // output buffer    MAX_STR);           // size of output buffer 

Execution of the previous code would give the following result on English (United States) and Danish user locales, respectively. (See Figure 4-19.)

figure 4-19 currency format for english (united states) and danish.

Figure 4-19 - 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.

 CURRENCYFMT CurFormat; // First fill in the CURRENCYFMT structure with user locale- specific //    information. GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER|LOCALE_IDIGITS,    &CurFormat.NumDigits, STR_LEN); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER|LOCALE_ILZERO,    CurFormat.LeadingZero, STR_LEN); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING,    &CurFormat.Grouping, STR_LEN); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL,    &CurFormat.lpDecimalSep, STR_LEN); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND,    &CurFormat.lpThousandSep, STR_LEN); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER|LOCALE_INEGCURR,    CurFormat.NegativeOrder, STR_LEN); GetLocaleInfo(LOCALE_USER_DEFAULT,    LOCALE_RETURN_NUMBER|LOCALE_ICURRENCY, CurFormat.PositiveOrder,    STR_LEN); // Set euro as the default currency symbol. CurFormat.lpCurrencySymbol = TEXT(""); GetCurrencyFormat(LOCALE_USER_DEFAULT, // a predefined value for                                        //    user locale    0,                    // operation option    TEXT("123.40"),       // input number (see MSDN for legal chars)    &CurFormat,           // formatting specifications    g_szTemp,             // output buffer    MAX_STR);             // size of output buffer 

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 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 Original = SetLocale("bg") DateData = FormatCurrency(3943.23) Original = SetLocale(currentLocale) 

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, theFormatCurrency function gives you the ability to display currency according to the user's cultural preferences, in both short and long currency formats.

Currency Formatting in the .NET Framework

Glossary


  • Windows Forms: The new platform for Microsoft Windows application development, based on the .NET Framework. Provides a clear, object-oriented, extensible set of classes that enables you to develop rich Windows applications. Additionally, Windows Forms can act as the local UI in a multitier, distributed solution.
  • ISO 4217 currency symbol: Three-letter ISO codes for representing currencies and funds (such as CAD for the Canadian dollar).

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; String MyString = MyInt.ToString("C"); Console.WriteLine(MyString); 

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.

 using System; using System.Globalization; using System.Threading; public class EuroSymbolSample {    public static void Main()    {       int i = 100;       // Set the CurrentCulture to French in France.       Thread.CurrentThread.CurrentCulture =   new CultureInfo("fr-FR");       // Display i formatted as default currency for the       //    CurrentCulture.       // On a version of Windows prior to Windows XP, where the user       //    has not changed the default currency to euro through       //    Control Panel, this will default to "F".       Console.WriteLine(i.ToString("c"));       // Set the CurrentCulture to French in France, using the       //    CultureInfo constructor that takes a useUserOverride       //    parameter.       // Set the useUserOverride value to false.       Thread.CurrentThread.CurrentCulture =          new CultureInfo("fr-FR", false); // Display i formatted as default currency for the        //    CurrentCulture.       // On a version of Windows prior to Windows XP, this will       //    override an incorrect default setting of "F" and display       //    the euro symbol (i).       Console.WriteLine(i.ToString("c"));         } } 

If you execute this code in a Windows Forms application, the output appears as follows:

    100,00 F

    100,00

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.

 using System; using System.Globalization; using System.Threading; public class EuroLocalSample {public static void Main()    {                   // Create a CultureInfo object for French in France.  CultureInfo FrCulture = new CultureInfo("fr-FR");  // Set the CurrentCulture to fr-FR.       Thread.CurrentThread.CurrentCulture = FrCulture;  // Clone the NumberFormatInfo object and create       //    a new object for the local currency of France.       NumberFormatInfo LocalFormat =          (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();       // Replace the default currency symbol with the local       //    currency symbol.       LocalFormat.CurrencySymbol = "F";       int i = 100;       // Display i formatted as the local currency.       Console.WriteLine(i.ToString("c", LocalFormat));  // Display i formatted as the default currency.       Console.WriteLine(i.ToString("c", NumberFormatInfo.CurrentInfo));    } } 

The following section will help you handle linguistic uppercasing and lowercasing rules that correspond to the currently selected user-locale value. As you'll see, whether you represent something as uppercase or lowercase might initially seem insignificant, but there can be considerable consequences to ignoring casing rules.



Microsoft Corporation - Developing International Software
Developing International Software
ISBN: 0735615837
EAN: 2147483647
Year: 2003
Pages: 198

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