CopyPastehas never been so tasty!

Format String into current & invariant culture in C#

by anonymous

  • 0
  • 0
  • 0

Orignal Source:


Current culture:


Current Culture property shows us the current culture information. This information finds the current format of dates, times, currency, numbers, the sorting order of text, string comparisons. We use a namepace System.Globalization for Current Culture. The current culture property does not concern with language it only concern with data and this property sets the standard setting of geographical region.


To understand let us consider this example in this we are formatting a string into Current Culture and displaying the date. This program will show the culture date and number.


To demonstrate this example make a console application writes the following code.








staticvoid Main(string[] args)



string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" };


DateTime dateToDisplay = newDateTime(2009, 9, 1, 18, 32, 0);

double number = 9164.32;


Console.WriteLine("Culture     Date                                number\n");

foreach (string cultureName in cultureNames)


CultureInfo culture = newCultureInfo(cultureName);

string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}",

                                              culture.Name, dateToDisplay, number);










Invariant culture:


The InvariantCulture is a property and this property is a culture but it is not particular or neutral. It is another type of culture. It supports only English language. We use a namepace System.Globalization for InvariantCulture. The Invariant Culture is used to store data that is not shown to the user means it is stored at back end. We store string data into culture format and it does not change but user can format when he access the string by different culture.


To understand let us consider an example in this we take a number of double type and initialize it. And then format this number into string by using CultureInfo.InvariantCulture


To demonstrate make a console application and write this code.








staticvoid Main(string[] args)



double number;


number = 123;


// Displays 00123


number = 1.2;

Console.WriteLine(number.ToString("0.00", CultureInfo.InvariantCulture));

// Displays 1.20

Console.WriteLine(number.ToString("00.00", CultureInfo.InvariantCulture));

// Displays 01.20



// Displays 01,20


number = .56;

Console.WriteLine(number.ToString("0.0", CultureInfo.InvariantCulture));

// Displays 0.6


number = 1234567890;

Console.WriteLine(number.ToString("0,0", CultureInfo.InvariantCulture));  

// Displays 1,234,567,890     



// Displays 1.234.567.890


number = 1234567890.123456;


// Displays 1,234,567,890.1 


number = 1234.567890;


// Displays 1,234.57






Add A Comment: