共用方式為


Formatting for Different Cultures 

For most methods, the values returned using one of the string format specifiers have the ability to dynamically change based on the current culture or a specified culture. For example, an overload of the ToString method accepts a format provider that implements the IFormatProvider interface. Classes that implement this interface can specify characters to use for decimal and thousand separators and the spelling and placement of currency symbols. If you do not use an override that takes this parameter, the ToString method will use characters specified by the current culture.

The following example uses the CultureInfo class to specify the culture that the ToString method and format string will use. This code creates a new instance of the CultureInfo class called MyCulture and initializes it to the French culture using the string fr-FR. This object is passed to the ToString method with the C string format specifier to produce a French monetary value.

Dim MyInt As Integer = 100
Dim MyCulture As New CultureInfo("fr-FR")
Dim MyString As String = MyInt.ToString("C", MyCulture)
Console.WriteLine(MyString)
int MyInt = 100;
CultureInfo MyCulture = new CultureInfo("fr-FR");
String MyString = MyInt.ToString("C", MyCulture);
Console.WriteLine(MyString);

The preceding code displays 100,00 when displayed in a Windows Forms form. Note that the console environment does not support all Unicode characters and displays 100,00 ? instead.

See the CultureInfo class for a list of all supported cultures.

The following example illustrates how to modify the CultureInfo object associated with the current thread. This sample assumes that U.S. English (en-US) is the culture associated with the current thread and shows how to change that culture through code. This sample also demonstrates how to specify a particular culture by passing a modified CultureInfo to a ToString method and how to pass a new DateTimeFormatInfo to a ToString method.

Dim dt As DateTime = DateTime.Now
Dim dfi As DateTimeFormatInfo = New DateTimeFormatInfo()
Dim ci As CultureInfo = New CultureInfo("de-DE")

' Create a new custom time pattern for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd"

' Use the DateTimeFormat from the culture associated with 
' the current thread.

Console.WriteLine( dt.ToString("d") ) 
Console.WriteLine( dt.ToString("m") )

' Use the DateTimeFormat object from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) )

' Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) )

' Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-BE")
Console.WriteLine( dt.ToString("d") )
DateTime dt = DateTime.Now;
DateTimeFormatInfo dfi = new DateTimeFormatInfo();
CultureInfo ci = new CultureInfo("de-DE");

// Create a new custom time pattern for demonstration.
dfi.MonthDayPattern = "MM-MMMM, ddd-dddd";

// Use the DateTimeFormat from the culture associated with 
// the current thread.
Console.WriteLine( dt.ToString("d") ); 
Console.WriteLine( dt.ToString("m") );

// Use the DateTimeFormat object from the specific culture passed.
Console.WriteLine( dt.ToString("d", ci ) );

// Use the settings from the DateTimeFormatInfo object passed.
Console.WriteLine( dt.ToString("m", dfi ) );

// Reset the current thread to a different culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-BE");
Console.WriteLine( dt.ToString("d") );

See Also

Reference

System.IFormatProvider Interface
System.Globalization.CultureInfo Class

Other Resources

Formatting Types