Formatting for Different Cultures
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
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.
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 example 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 example also demonstrates how to specify a particular culture by passing a modified CultureInfo to a ToStringmethod and how to pass a new DateTimeFormatInfo to a ToString method.
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") ); // This example produces the following output: // 3/27/2008 // March 27 // 27.03.2008 // 03-March, Thu-Thursday // 27/03/2008