A culture is the set of language and region conventions we use to represent data such as dates, numbers, and currencies.
In C#, the CultureInfo class, within the System.Globalization namespace, allows applying these conventions without rewriting the application logic.
What a Culture Represents
In programming, the concept of culture refers to the combination of several factors that affect formats and conventions in a given region. These factors include:
- Language: The primary language spoken in the region (e.g., English, Spanish, French).
- Regional conventions: Include how dates, numbers, times, and currencies are displayed.
- Data format: Such as the decimal separator, use of commas or periods for thousands, and the currency format.
In C#, the CultureInfo class provides all the necessary functionality to work with cultures, allowing an application to be adapted to different languages and regions without needing to rewrite the core code.
The CultureInfo Class
The CultureInfo class is part of the System.Globalization namespace and allows representing a culture or regional setting. A CultureInfo instance provides access to information about a specific language and its regional conventions, such as currency, dates, and numbers.
Creating a CultureInfo Instance
To create a CultureInfo instance, you can use the constructor by passing a culture code, which is a string representing a specific culture. Culture codes are standard identifiers like "en-US" (English - United States) or "es-ES" (Spanish - Spain).
using System;
using System.Globalization;
class Program
{
static void Main()
{
// Create a CultureInfo instance for the United States
CultureInfo cultureUS = new CultureInfo("en-US");
// Create a CultureInfo instance for Spain
CultureInfo cultureES = new CultureInfo("es-ES");
Console.WriteLine("US Culture: " + cultureUS.Name);
Console.WriteLine("ES Culture: " + cultureES.Name);
}
}In this example, two CultureInfo instances are created: one for the United States and another for Spain. The Name method returns the full culture name, which includes the language and country.
Properties of CultureInfo
Some of the key properties of the CultureInfo class are:
- Name: The full name of the culture (e.g.,
"en-US"). - DisplayName: The culture’s name in a readable format (e.g.,
"English (United States)"). - DateTimeFormat: A
DateTimeFormatInfoobject containing the date and time conventions for that culture. - NumberFormat: A
NumberFormatInfoobject that defines the conventions for number representation. - NumberFormat.CurrencySymbol: The currency symbol for the specified culture.
Using the NumberFormat Property
The NumberFormat property of CultureInfo allows access to the conventions for displaying numbers in a specific culture. Through this property, you can obtain information about the decimal separator, the thousands symbol, and other numeric formatting details.
using System;
using System.Globalization;
class Program
{
static void Main()
{
// Create a CultureInfo instance for Germany
CultureInfo cultureDE = new CultureInfo("de-DE");
// Display the decimal separator and thousands separator
Console.WriteLine("Decimal separator in Germany: " + cultureDE.NumberFormat.NumberDecimalSeparator);
Console.WriteLine("Thousands separator in Germany: " + cultureDE.NumberFormat.NumberGroupSeparator);
}
}In this case, for the German culture (de-DE), numbers have a different format than the US culture, as Germany uses a comma as the decimal separator and a period for thousands.
Changing Culture at Runtime
In applications that need to dynamically switch between different regional settings, you can change the current thread’s culture. This is useful, for example, when a user changes the language in an application.
using System;
using System.Globalization;
using System.Threading;
class Program
{
static void Main()
{
// Change the thread's culture to Spanish (Mexico)
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX");
// Display the date in the current culture's format
Console.WriteLine(DateTime.Now.ToString("D"));
}
}In this example, the thread’s culture is changed to es-MX (Spanish – Mexico), and the date is displayed according to the local conventions of that region.
Formatting Dates and Numbers
With the CultureInfo class, it is easy to format dates and numbers according to the conventions of a specific culture. The ToString method on types like DateTime and Double can accept a CultureInfo object to adapt the format.
Formatting Dates
using System;
using System.Globalization;
class Program
{
static void Main()
{
// Create a date
DateTime date = new DateTime(2024, 11, 8);
// Format the date according to the US culture
CultureInfo cultureUS = new CultureInfo("en-US");
Console.WriteLine("Date in US: " + date.ToString("D", cultureUS));
// Format the date according to the Spanish culture
CultureInfo cultureES = new CultureInfo("es-ES");
Console.WriteLine("Date in Spain: " + date.ToString("D", cultureES));
}
}Here, the date is presented in different ways depending on the configured culture. The D format generates a long date with the order and proper names specific to each culture.
Formatting Numbers
using System;
using System.Globalization;
class Program
{
static void Main()
{
// Create a number
double number = 1234567.89;
// Format the number according to the Japanese culture
CultureInfo cultureJP = new CultureInfo("ja-JP");
Console.WriteLine("Number in Japan: " + number.ToString("N", cultureJP));
// Format the number according to the German culture
CultureInfo cultureDE = new CultureInfo("de-DE");
Console.WriteLine("Number in Germany: " + number.ToString("N", cultureDE));
}
}This code shows how the same number is presented differently depending on the culture: in Japan, commas are used as thousands separators, while in Germany, a period is used.