Culture and formatting

A culture decides how a number and a date look as text: the decimal separator, the thousands separator, the order of the parts of a date, the currency symbol, the alphabet of a sort. The same double printed with two cultures is two different strings, and a program that chooses none gets the culture of the process.

Invariant and named cultures

CultureInfo.InvariantCulture is the neutral one: a dot for the decimal separator, a comma for the thousands, the ISO date order, no currency of any country. A named culture follows the rules of that language:

using System.Globalization; var value = 1234567.891; var date = new DateTime(2024, 3, 10, 14, 5, 0); Console.WriteLine(value.ToString("N2", CultureInfo.InvariantCulture)); Console.WriteLine(value.ToString("N2", new CultureInfo("en-US"))); Console.WriteLine(value.ToString("N2", new CultureInfo("de-DE"))); Console.WriteLine(value.ToString("C2", new CultureInfo("en-US"))); Console.WriteLine(date.ToString("d", CultureInfo.InvariantCulture)); Console.WriteLine(date.ToString("d", new CultureInfo("de-DE"))); Console.WriteLine(date.ToString("D", new CultureInfo("en-US"))); Console.WriteLine($"{value:N2} is the current culture");

dotnet run

1,234,567.89
1,234,567.89
1.234.567,89
$1,234,567.89
03/10/2024
10.03.2024
Sunday, March 10, 2024
1,234,567.89 is the current culture

The invariant and the American formats agree on this value, and the German one swaps the separators: the dot groups the thousands and the comma marks the decimals. The same date is 03/10/2024 under the invariant culture, 10.03.2024 in German and a weekday in a long American form. The last line interpolated with no culture and got the invariant one, which is the subject of the next section.

The current culture

A process starts with a culture taken from the environment, and a program may change it. This container has no locale variable set, so the process starts in the invariant culture, whose name is empty:

using System.Globalization; Console.WriteLine($"culture {CultureInfo.CurrentCulture.Name}"); CultureInfo.CurrentCulture = new CultureInfo("de-DE"); Console.WriteLine($"culture {CultureInfo.CurrentCulture.Name}"); Console.WriteLine($"number {1234.5:N2}"); Console.WriteLine($"date {new DateTime(2024, 3, 10):d}"); Console.WriteLine($"invariant {1234.5.ToString("N2", CultureInfo.InvariantCulture)}");

dotnet run

culture 
culture de-DE
number 1.234,50
date 10.03.2024
invariant 1,234.50

The first line is empty after the word, because the invariant culture has no name. After the assignment every interpolation without an explicit culture follows the German rules, while the last line, which names the invariant culture, does not. The same program started with LANG=de_DE.UTF-8 prints de-DE on the first line, which was measured here: the runtime reads the locale variables of the system.

The rule for a program is short. A file, a protocol and a log use the invariant culture, so another machine reads them the same way; text for a person uses the current culture. A program that mixes the two writes a file that only its own machine can read.

Sorting and comparing

Sorting has the same choice. An ordinal comparison compares the code units of the characters, and a culture comparison applies the rules of a language, where case and accents order differently:

using System.Globalization; var words = new[] { "apple", "Banana", "cherry" }; Array.Sort(words, StringComparer.Ordinal); Console.WriteLine($"ordinal {string.Join(",", words)}"); Array.Sort(words, StringComparer.InvariantCultureIgnoreCase); Console.WriteLine($"ignore case {string.Join(",", words)}"); Console.WriteLine($"compare {string.Compare("a", "B", StringComparison.Ordinal)}"); Console.WriteLine($"compare {string.Compare("a", "B", StringComparison.OrdinalIgnoreCase)}"); Console.WriteLine($"equals {string.Equals("STRASSE", "straße", StringComparison.OrdinalIgnoreCase)}");

dotnet run

ordinal Banana,apple,cherry
ignore case apple,Banana,cherry
compare 31
compare -1
equals False

Ordinal order puts the capital B before the lowercase a, because 66 is below 97; the culture-aware comparison ignores case and puts apple first. The two comparisons of a and B differ by exactly that: 31 is the difference of the code units, and the culture-aware one answers that a sorts before B. ß and SS are equal in a German collation but not ordinally, which the last line shows.

Ordinal for identifiers

Exercises

  1. Print the same number with fr-FR and ja-JP and compare the separators.
  2. Format a price for a German and an American reader and note where the currency symbol goes.
  3. Sort a list of three names with the ordinal and the culture comparer and find one that swaps.
  4. Run the second program with LANG=fr_FR.UTF-8 and read the first line.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the invariant culture?

    Answer

    A neutral culture with fixed formats and no country

    It is the reference format of the framework, meant for files and protocols.

  2. Why does a program format a file with the invariant culture?

    Answer

    So another machine reads the same text

    A culture-dependent separator makes a file ambiguous on another machine.

  3. What does this program print? using System.Globalization; Console.WriteLine(1234.5.ToString("N2", CultureInfo.InvariantCulture));

    Answer

    1,234.50

    The invariant culture groups with a comma and marks decimals with a dot.

  4. What does this program print? using System.Globalization; Console.WriteLine(new CultureInfo("de-DE").NumberFormat.NumberDecimalSeparator);

    Answer

    ,

    German writes the decimal separator as a comma.

  5. Where does the current culture of a process come from at startup?

    Answer

    The locale variables of the environment

    The runtime reads the locale of the system, which can be unset as it was here.

  6. What is an ordinal comparison?

    Answer

    A comparison of the code units with no culture rules

    It is the byte-like order of the characters, which is why B comes before a.

  7. What does this program print? Console.WriteLine(string.Compare("a", "B", StringComparison.Ordinal));

    Answer

    31

    The code unit of a is 97 and of B is 66, and 97 minus 66 is 31.

  8. What does the format C2 add to a number?

    Answer

    The currency symbol and two decimals

    The measured American output began with the dollar sign.

  9. What does this program print? Console.WriteLine(string.Equals("A", "a", StringComparison.Ordinal));

    Answer

    False

    Ordinal comparison is case-sensitive.

  10. Which comparison fits a dictionary key or a protocol header?

    Answer

    Ordinal

    An identifier must mean the same bytes on every machine, so no culture rules apply.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev