Home  


Getting Culture specific Information in CSharp

Always need to use cultures when appropriate, but need to be careful when using them. They are meant to be specific for a user or machine, and they could change. For example, as countries join the EU, their currency symbols or formats change. In other cases, companies or users may prefer a 24hour clock to a 12hour default.

Culture data is necessary and great when presenting data to the humans who are actually using the computer, but it’s an awful way to store data.

using System;
using System.Globalization;
/// 
/// Sample demonstrating the use of the CultureInfo class.
/// Use this class to retrieve information about a specific culture, such as
/// date/time formats and number formats.
/// 
internal class CultureInfoSample
{
	private static void Main()
	{
		Console.WriteLine("Summary for the current thread culture:");
		WriteCultureInfoSummary(CultureInfo.CurrentCulture);
		Console.WriteLine("Summary for the current thread UI culture:");
		WriteCultureInfoSummary(CultureInfo.CurrentUICulture);
		Console.WriteLine("Summary for the operating system UI culture:");
		WriteCultureInfoSummary(CultureInfo.InstalledUICulture);
		Console.WriteLine();
		Console.WriteLine();
		Console.WriteLine("Press Enter to continue");
		Console.ReadLine();
	}
	
	// Writes a summary of the specified CultureInfo to the console.
	private static void WriteCultureInfoSummary(CultureInfo info)
	{
		Console.WriteLine(" Culture name: {0} ({1})",info.DisplayName, info.Name);
		WriteDateTimeFormatInfoSummary(info.DateTimeFormat);
		WriteNumberFormatInfoSummary(info.NumberFormat);
		Console.WriteLine();
    	}
	
	// Writes a summary of the specified DateTimeFormatInfo to the console.
	private static void WriteDateTimeFormatInfoSummary(DateTimeFormatInfo info)
	{
		Console.WriteLine(" Long date/time pattern: {0} {1}",
		info.LongDatePattern, info.LongTimePattern);
		Console.WriteLine(" Short date/time pattern: {0} {1}",
		info.ShortDatePattern, info.ShortTimePattern);
		Console.WriteLine(" AM/PM designators: {0}/{1}",
		info.AMDesignator, info.PMDesignator);
	}
	
	// Writes a summary of the specified NumberFormatInfo to the console.
	private static void WriteNumberFormatInfoSummary(NumberFormatInfo info)
	{
		Console.WriteLine(" Currency symbol: {0}", info.CurrencySymbol);
		Console.WriteLine(" Percent symbol: {0}", info.PercentSymbol);
		Console.WriteLine(" Negative sign: {0}", info.NegativeSign);
		Console.WriteLine(" Positive sign: {0}", info.PositiveSign);
		Console.WriteLine(" Decimal separator: {0}", info.NumberDecimalSeparator);
		Console.WriteLine(" Thousands separator: {0}", info.NumberGroupSeparator);
	}

 }

}

			
					
					



The Ouput looks like