格式化特定文化特性的數值資料
NumberFormatInfo 類別定義如何根據文化特性來格式化和顯示貨幣、小數分隔符號和其他數值符號。例如,十進位數字將文化特性 "en-US" 的 10000.50 格式化為 10,000.50,並將文化特性 "de-DE" 的格式化為 10.000,50。
您可以為特定文化特性或不變文化特性 (但非中性文化特性) 建立 NumberFormatInfo 的執行個體。中性文化特性並未提供足夠的資訊來顯示正確的數值格式。如果您嘗試使用中性文化特性來建立 NumberFormatInfo 的執行個體,將會擲回例外狀況。
下列程式碼範例使用 CurrentCulture 的 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
格式化歐元國家貨幣
CultureInfo 和 RegionInfo 類別皆包含貨幣資訊。只有一個貨幣是根據文化特性來指定。歐元是下列國家的官方貨幣:比利時、德國、西班牙、法國、愛爾蘭、義大利、盧森堡、荷蘭、奧地利、葡萄牙、芬蘭和希臘。自 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 物件,傳遞 useUserOverride 參數設定為 false。
下列範例使用的程式碼與前一個範例相似。該程式碼將目前的文化特性設為 "fr-FR" 並將格式化為貨幣的整數顯示給主控台。它將使用使用者的區域貨幣設定來格式化貨幣。接著,使用接受設定為 false 之 useUserOverride 參數的 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 範例>。