Plunging into .NET Development

Weblog Pieter Gheysens
Microsoft .NET Development - C# - Enterprise Library - Visual Studio 2005 Team System - Compuware DevPartner - ...
 


Sunday, July 29

custom Formatting with CultureInfo

The CultureInfo class in .NET provides information about a specific culture (also known as a locale). The information in the culture includes the names for the culture, the writing system, the calendar used, and formatting for dates, currency and numbers.

At start-up of an application you should always set the desired CultureInfo to the running thread to avoid wrong formatting of DatetTime and Number information in your application. Especially if you need custom formatting ... because by default, the running thread takes the culture settings of the machine it is running on. In the example below I set some custom fixed formatting options for DateTime and numbers. Afterwards I assign this cultureInfo to the CurrentCulture of the current thread.

    //create custom cultureInfo based on nl-be

    CultureInfo cultureInfo = new CultureInfo("nl-be");

    //DateTimeFormatting

    cultureInfo.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";

    cultureInfo.DateTimeFormat.ShortTimePattern = "HH:mm";

    //NumberFormatting

    cultureInfo.NumberFormat.NumberDecimalDigits = 2;

    cultureInfo.NumberFormat.NumberGroupSeparator = " ";

    cultureInfo.NumberFormat.NumberDecimalSeparator = ",";

 

    Thread.CurrentThread.CurrentCulture = cultureInfo;

 

    DateTime testDateTime = DateTime.UtcNow;

    decimal testDecimal = 123456.789012M;

 

    StringBuilder sbOutput = new StringBuilder();

    sbOutput.Append("Date : " + testDateTime.ToShortDateString());

    sbOutput.Append(Environment.NewLine);

    sbOutput.Append("Time : " + testDateTime.ToShortTimeString());

    sbOutput.Append(Environment.NewLine);

    sbOutput.Append("testDecimal : " + testDecimal.ToString("N"));

 

    MessageBox.Show(sbOutput.ToString());


As a result, the ToShortDateString method on the DateTime object will always display the DateTime information in the desired format. Also the number formatting for the decimal is like it should be. Note that I must pass the numeric format N to the ToString method so that the decimal will be shown as a number. More information about standard numeric format strings at MSDN.



If you need formatting that deviates from the default formatting specified in the current thread, you can still specify a custom FormatProvider in the ToString method.

    NumberFormatInfo nfi = new NumberFormatInfo();

    nfi.NumberDecimalDigits = 4;

    nfi.NumberGroupSeparator = ",";

    nfi.NumberDecimalSeparator = ".";

 

    decimal testDecimal = 123456.789012M;

    MessageBox.Show(testDecimal.ToString("N", nfi));




My advice : do not create your own custom formatting/conversion functions in your applications, but try to keep it simple and use the stuff that's already available in the .NET Framework (CultureInfo class)!

One extra remark on cultures on the CurrentThread :
  • CurrentUICulture specifies the culture for the user interface and is used for resource file lookups.
  • CurrentCulture specifies the culture for data and information. It is used to format dates, numbers, currencies and sort strings.

Labels:

0 Comments:

Post a Comment

<< Home