针对特定区域性格式化数值数据
更新:2007 年 11 月
NumberFormatInfo 类定义如何根据区域性来格式化和显示货币、小数点分隔符和其他数值符号。例如,对于区域性英语(美国)“en-US”,将十进制数字 10000.50 格式化为 10,000.50;对于区域性德语(德国)“de-DE”,则将其格式化为 10.000,50。
只能为特定区域性或固定区域性创建 NumberFormatInfo 对象,而不能为非特定区域性创建该对象。非特定区域性不提供显示正确数字格式所需的足够信息。如果应用程序尝试使用非特定区域性来创建 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
设置欧元格式
CultureInfo 和 RegionInfo 类都包含有关货币的信息。为每一个区域性仅指定了一种货币。欧元是以下国家/地区的官方货币:比利时、德国、西班牙、法国、爱尔兰、意大利、卢森堡、荷兰、奥地利、葡萄牙、芬兰和希腊。2002 年 1 月 1 日,这些国家/地区开始使用欧元纸币和硬币。因此,对于这十二个国家/地区,.NET Framework 和 Microsoft Windows XP 将默认货币符号设置为欧元。Windows 的早期版本仍将默认货币符号设置为这些国家/地区的本地货币。
您应注意各操作系统在默认货币符号方面存在的差异。在 Windows 中,用户可以通过“控制面板”中的“区域和语言选项”来重写与操作系统默认区域性相关联的某些值。例如,用户可以选择用该区域性的默认值以外的符号来显示货币。Windows 窗体和控制台应用程序使用操作系统中指定的默认货币符号。对于采用欧元的国家/地区中使用 Windows 早期版本的用户而言,如果该用户尚未通过“控制面板”中的“区域和语言选项”将货币设置更新为欧元,其默认货币设置将不正确。ASP.NET 应用程序以及在 ASP.NET 中创建的 XML Web services 应用程序是作为系统服务运行的,而不是为特定用户运行的。因此,它们返回的默认结果可能与 Windows 窗体和控制台应用程序返回的结果不一样。
建议您编写对区域性使用 .NET Framework 默认货币设置的代码,以使应用程序不会受到操作系统差异所带来的影响,并确保货币格式化的一致性。应用程序应使用接受 useUserOverride 参数的构造函数重载之一来创建 CultureInfo 对象,并将此参数设置为 false。此设置会使用户操作系统上的默认货币设置被 .NET Framework 的正确默认设置重写。
在下面的 XML Web services 代码中,UserLocalSetting XML Web services 方法将 CurrentCulture 属性设置为法语(法国)“fr-FR”,并检索被格式化为货币形式的整数。由于操作系统所具有的差异,无法确定是使用欧元符号还是“F”符号。OverrideUserSetting XML Web 服务器方法使用接受 useUserOverride 参数(其设置为 false)的 CultureInfo 构造函数将 CurrentCulture 属性设置为“fr-FR”。此方法检索被格式化为货币形式的整数。在此情况下,可以保证使用欧元符号,因为这是 .NET Framework 的默认设置。
[Visual Basic]
<%@ 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 窗体和控制台应用程序通过用户计算机的设置来设置默认货币符号。如前所述,该设置可能不正确。若要确保使用 .NET Framework 默认设置,应用程序必须创建一个 CultureInfo 对象,并传递一个被设置为 false 的 useUserOverride 参数。
下面的示例使用类似于前一个示例的代码。它将当前区域性设置为“fr-FR”,并将一个格式化为货币的整数显示到控制台。使用用户的本地货币设置格式化该货币。接着,使用接受被设置为 false 的 useUserOverride 参数的 CultureInfo 构造函数将区域性设置为“fr-FR”。然后,使用 .NET Framework 的默认设置格式化该数字,此时将显示欧元货币符号。
[Visual Basic]
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 窗体应用程序中执行这些代码,则输出结果为:
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));
}
}
有关相关示例,请参见“常见任务快速入门”中的“多货币示例”。