共用方式為


格式化特定文化特性的數值資料

NumberFormatInfo 類別定義如何根據文化特性來格式化和顯示貨幣、小數分隔符號和其他數值符號。 例如,在英文 (美國) "en-US" 文化特性中,十進位數字 10000.50 會格式化為 10,000.50,在德文 (德國) "de-DE" 文化特性中,會格式化為 10.000,50。

NumberFormatInfo 物件可以為特定文化特性 (Culture) 或不因文化特性而異 (Invariant Culture) 建立,但不可為中性文化特性建立。 中性文化特性並未提供足夠的資訊來顯示正確的數值格式。 如果應用程式嘗試使用中性文化特性來建立 NumberFormatInfo 物件,將會擲回例外狀況。

下列程式碼範例會使用目前文化特性的 NumberFormatInfo 標準貨幣格式 ("c") 來顯示整數。

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

這個程式碼產生下列輸出:

BZ$100.00
$100.00
kr100,00

格式化歐元國家貨幣

CultureInfoRegionInfo 類別都包含貨幣的資訊。 只有一個貨幣是根據文化特性來指定。 歐元是下列國家的官方貨幣:比利時、德國、西班牙、法國、愛爾蘭、義大利、盧森堡、荷蘭、奧地利、葡萄牙、芬蘭和希臘。 自 2002 年 1 月 1 日起,這些國家已開始使用歐元銀行紙幣和硬幣。 因此,.NET Framework 和 Microsoft Windows XP 會為這十二個國家的預設貨幣符號設為歐元。 舊版 Windows 仍會將預設貨幣符號設為這些國家的當地貨幣。

您必須注意作業系統的預設貨幣符號差異處。 在 Windows 中,使用者可以透過 [控制台] 中的地區及語言選項,來覆寫作業系統預設文化特性的某些值。 例如,使用者可以選擇使用符號 (而不是文化特性的預設值) 來顯示貨幣。 Windows Form 和主控台應用程式 (Console Application) 使用作業系統所指定的預設貨幣符號。 在舊版 Windows 中,如果歐元使用國家或地區的使用者並未將貨幣更新為歐元 (透過 [控制台] 的地區及語言選項),其預設貨幣設定將會是錯誤的。 ASP.NET 應用程式和 ASP.NET 中建立的 XML Web Service 應用程式將做為系統服務來執行,而不是針對特定使用者來執行。 因此,它們傳回的預設結果將和 Windows Form 和主控台應用程式傳回的結果不同。

建議您替文化特性撰寫使用 .NET Framework 預設貨幣設定的程式碼,來保護您的應用程式不受作業系統的差異影響,並確保擁有一致的貨幣格式。 您的應用程式應使用其中一個接受 useUserOverride 參數的建構函式多載,並將這個參數設為 false,來建立 CultureInfo 物件。 這個設定將使得使用者作業系統的預設貨幣設定覆寫為 .NET Framework 的正確預設值。

在下列 XML Web Service 程式碼中,UserLocalSetting XML Web Service 方法將 CurrentCulture 屬性設為法文 (法國) "fr-FR",並擷取格式化為貨幣的整數。 由於作業系統的差異,您無法確定是否使用歐元符號或 "F" 符號。 OverrideUserSetting XML Web 伺服器方法使用接受 useUserOverride 參數並將其設定為 false 的 CultureInfo 建構函式,將 CurrentCulture 屬性設為 "fr-FR"。 這個方法會擷取貨幣格式的整數。 在此情況下,由於歐元符號是 .NET Framework 的預設值,因此保證會使用歐元符號。

<%@ 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 作業系統所有版本的 Windows Form 和主控台應用程式設定使用者電腦的設定值的預設貨幣符號。 如前所述,該設定可能會有錯。 若要確保使用 .NET Framework 的預設設定,您的應用程式必須建立 CultureInfo 物件,並傳遞設定為 false 的useUserOverride 參數。

下列範例使用的程式碼與前一個範例相似。 該程式碼將目前的文化特性設為 "fr-FR" 並將格式化為貨幣的整數顯示給主控台。 它將使用使用者的區域貨幣設定來格式化貨幣。 接著,文化特性會使用接受 useUserOverride 參數並將其設為 false 的 CultureInfo 建構函式,來設定為 "fr-FR"。 然後使用 .NET Framework 預設設定來格式化這個數字,並顯示歐元貨幣符號。

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

請注意,主控台環境不支援歐元字元。 如果您在 Windows Form 應用程式中執行這個程式碼,它將產生下列輸出:

100,00 F
100,00 €

許多歐洲國家使用兩種通用的貨幣:歐元和區域貨幣。 有時候使用者必須同時在應用程式顯示這兩種貨幣。 下列程式碼範例為文化特性 "fr-FR" 建立 CultureInfo 物件,其預設貨幣為歐元。 若要顯示本地貨幣的貨幣符號,您必須使用 NumberFormatInfo.Clone 方法為 CultureInfo 複製新的 NumberFormatInfo,然後使用本地貨幣符號來取代預設貨幣符號。

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

如需相關範例,請參閱通用工作快速入門中的<MultiCurrency 範例>。

請參閱

其他資源

編碼和當地語系化

Formatting Types

Formatting for Different Cultures