Partager via


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 "en-US" and 10.000,50 for the culture "de-DE".

An instance of NumberFormatInfo 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 you attempt to create an instance of NumberFormatInfo using a neutral culture.

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

Imports System
Imports System.Globalization

Public Class TestClass

   Public Shared Sub Main()
      Dim i As Integer = 100
      
      ' Creates a CultureInfo for English in Belize.
      Dim bz As 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.
      Dim us As New CultureInfo("en-US")
      ' Displays i formatted as currency for us.
      Console.WriteLine(i.ToString("c", us))
      
      ' Creates a CultureInfo for Danish in Denmark.
      Dim dk As New CultureInfo("da-DK")
      ' Displays i formatted as currency for dk.
      Console.WriteLine(i.ToString("c", dk))
   End Sub
End Class
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

Formatting Currency for Euro Nations

The CultureInfo and the 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 using the euro as the official currency. Older versions of Windows will still set the default currency symbol to the local currency for these nations.

You should be aware of operating system differences with respect to default currency symbols. In Windows, users are able to override some of the values associated with the system's default culture through Regional and Language Options (or Regional Options or Regional Settings) 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 user's 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 Options or Regional Settings 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.

It is recommended that you write code that uses the .NET Framework default currency settings for a culture to shield your application from operating systems differences and ensure consistent currency formatting. Create a CultureInfo using one of its constructor overloads that accepts a useUserOverride parameter, and set this parameter to false. This will cause the default currency setting on the user's system to be overridden by the .NET Framework's correct default setting.

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

<%@ WebService Language="VB" Class="userOverrideSample" %>

Imports System
Imports System.Web.Services
Imports System.Globalization
Imports System.Threading

Public Class userOverrideSample

   <WebMethod> _
   Public Function UserLocalSetting() As String
      Dim i As Integer = 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"))
   End Function

   <WebMethod> _
   Public Function OverrideUserSetting() As String
      Dim i As Integer = 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"))
   End Function 
End Class
<%@ 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. If you want to ensure that your application uses the .NET Framework's default 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.

Imports System
Imports System.Globalization
Imports System.Threading

Public Class EuroSymbolSample
   Public Shared Sub Main()
      Dim i As Integer = 100
      
      ' Sets the CurrentCulture to French in France.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      ' Displays i formatted as 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"))
   End Sub
End Class
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 €

Many 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.

Imports System
Imports System.Globalization
Imports System.Threading

Public Class EuroLocalSample
   Public Shared Sub Main()
      ' Creates a CultureInfo for French in France.
      Dim FrCulture As 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.
      Dim LocalFormat As NumberFormatInfo =_
         CType(NumberFormatInfo.CurrentInfo.Clone(), NumberFormatInfo)
      ' Replaces the default currency symbol 
      ' with the local currency symbol.
      LocalFormat.CurrencySymbol = "F"
      
      Dim i As Integer = 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))
   End Sub
End Class 
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.

See Also

Concepts

Formatting For Different Cultures

Other Resources

Encoding and Localization
Formatting Types