Export (0) Print
Expand All

Formatting Numeric Data for a Specific Culture

The NumberFormatInfo class defines how currency, decimal separators, and other numeric symbols are formatted and displayed based on culture. For example, the decimal number 10000.50 is formatted as 10,000.50 for the culture English (United States), "en-US", and 10.000,50 for the culture German (Germany), "de-DE".

A NumberFormatInfo object can be created for a specific culture, or the invariant culture, but not for a neutral culture. A neutral culture does not provide enough information to display the correct numeric format. An exception is thrown if the application attempts to create a NumberFormatInfo object using a neutral culture.

The following code example displays an integer using the NumberFormatInfo standard currency format ("c") for the current culture.

using System;
using System.Globalization;

public class TestClass
{
   public static void Main()
   {
      int i = 100;
      
      // Creates a CultureInfo for English in Belize.
      CultureInfo bz = new CultureInfo("en-BZ");
      // Displays i formatted as currency for the bz.
      Console.WriteLine(i.ToString("c", bz));
      
      // Creates a CultureInfo for English in the U.S.
      CultureInfo us = new CultureInfo("en-US");
      // Display i formatted as currency for us.
      Console.WriteLine(i.ToString("c", us));
      
      // Creates a CultureInfo for Danish in Denmark.
      CultureInfo dk = new CultureInfo("da-DK");
      // Displays i formatted as currency for dk.
      Console.WriteLine(i.ToString("c", dk));
   }
}

This code produces the following output:

BZ$100.00
$100.00
kr100,00

The CultureInfo and RegionInfo classes both include information on currency. Only one currency is specified per culture. The euro is the official currency of Belgium, Germany, Spain, France, Ireland, Italy, Luxembourg, Netherlands, Austria, Portugal, Finland, and Greece. On January 1, 2002, these nations started using euro bank notes and coins. Therefore, the .NET Framework and Microsoft Windows XP set the default currency symbol to the euro for these twelve nations. Older versions of Windows still set the default currency symbol to the local currency for these nations.

You should be aware of operating system differences among default currency symbols. In Windows, users are able to override some of the values associated with the operating system default culture through the regional and language options in Control Panel. For example, a user can choose to display currency using a symbol other than the default for the culture. Windows Forms and console applications use the default currency symbol specified on the operating system. A user in a euro-adopting country or region, on an older version of Windows, who has not updated the currency setting to the euro through the regional and language options in Control Panel, will have an incorrect default currency setting. ASP.NET applications and XML Web service applications created in ASP.NET run as system services, not for specific users. Therefore, the default results they return might differ from those returned by Windows Forms and console applications.

You are recommended to write code that uses the .NET Framework default currency settings for a culture to shield your application from operating system differences and to ensure consistent currency formatting. Your application should create a CultureInfo object using one of the constructor overloads that accepts a useUserOverride parameter, and set this parameter to false. This setting causes the default currency setting on the user's operating system to be overridden by the correct default setting for the .NET Framework.

In the following XML Web service code, the UserLocalSetting XML Web service method sets the CurrentCulture property to French (France), "fr-FR", and retrieves an integer formatted as currency. Due to operating system differences, you cannot be sure whether the euro symbol or the "F" symbol is used. The OverrideUserSetting XML Web server method sets the CurrentCulture property to "fr-FR" using the CultureInfo constructor that accepts a useUserOverride parameter with a setting of false. This method retrieves an integer formatted as currency. In this case, you are guaranteed that the euro symbol will be used because it is the .NET Framework default.

<%@ WebService Language="c#" Class="userOverrideSample" %>

using System;
using System.Web.Services;
using System.Globalization;
using System.Threading;

public class userOverrideSample
{
   [WebMethod]
   public String UserLocalSetting()
   {
      int i = 100;

      // Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      // Displays i formatted as currency for the CurrentCulture.
      // Due to operating system differences, you cannot be sure what currency
      // symbol will be used.
      return (i.ToString("c"));
   }   
   
   [WebMethod]
   public String OverrideUserSetting()
   {
      int i = 100;

      // Sets the CurrentCulture to French in France.
      // Uses the CultureInfo constructor that takes a 
      // useUserOverride parameter.
      // Sets the useUserOverride value to false.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", _
         false);
      // Displays i formatted as currency for the CurrentCulture.
      // This will override any user settings and display the euro symbol.
      return (i.ToString("c"));
   }
}

Windows Forms and console applications running on all versions of Windows operating systems set the default currency symbol from the settings on the user's computer. As mentioned earlier, this setting might be incorrect. To ensure use of the .NET Framework default settings, your application 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;

      // Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      // Displays 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 the
      // Control Panel, this will default to "F".
      Console.WriteLine(i.ToString("c"));

      // Sets the CurrentCulture to French in France, using the
      // CultureInfo constructor that takes a useUserOverride parameter.
      // Sets the useUserOverride value to false. 
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR", 
            false);
      // Displays 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 ().
      Console.WriteLine(i.ToString("c"));      
   }
} 

Note that the console environment does not support the euro character. If you execute this code in a Windows Forms application, the output appears as follows:

100,00 F
100,00 €

Some European countries have two currencies in common use: the euro and the local currency. There might be situations where it is necessary to display both currencies in an application. The following code example creates a CultureInfo object for the culture "fr-FR" where the default currency is euro. In order to display the currency symbol for the local currency, you must use the NumberFormatInfo.Clone method to clone a new NumberFormatInfo for the CultureInfo 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()
   {             
      // Creates a CultureInfo for French in France.
      CultureInfo FrCulture = new CultureInfo("fr-FR");
      // Sets the CurrentCulture to fr-FR.
      Thread.CurrentThread.CurrentCulture = FrCulture;

      // Clones the NumberFormatInfo and creates
      // a new object for the local currency of France.
      NumberFormatInfo LocalFormat = 
         (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
      // Replaces the default currency symbol with the 
      // local currency symbol.
      LocalFormat.CurrencySymbol = "F";

      int i = 100;

      // Displays i formatted as the local currency.
      Console.WriteLine(i.ToString("c", LocalFormat));

      // Displays i formatted as the default currency.
      Console.WriteLine(i.ToString("c", NumberFormatInfo.CurrentInfo));
   }
}

For a related example, see the MultiCurrency Sample in the Common Tasks QuickStart.

Community Additions

ADD
Show:
© 2015 Microsoft