Skip to main content

Globalization Step-by-Step


Currency Formatting

*

Overview and Description

Currency formatting needs to take into consideration these following elements:

  • Currency symbol -- This can be a pre-defined symbol like the European Euro '€' or a combination of letters like the use of 'GBP' for British Pound.
  • Currency symbol placement -- It can be either place before or after the digits.
  • Negative-amount display -- Several of the ways to display negative amounts are:
    DescriptionCountryFormatting
    The negative sign before both the currency symbol and number.UK
    France
    -£127.54
    -127,54 F
    The negative sign before the number but behind the currency symbol.Denmarkkr-127,54
    The negative sign after the number.Netherlands€ 127,54-
    The use of parentheses.US($127.54)

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

Figure 1: Currency formatting in Regional Options


 

Top of pageTop of page

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 2.)

Figure 2 - Currency format for English (United States) and Danish.

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.

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

Top of pageTop of page

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;
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));
}
}


Top of pageTop of page

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


Top of pageTop of page

References

 

 

Top of pageTop of page Previous6 of 11 Next