skip to content

In C#, why can $"{total:N2}" print 1,234.50 on one server and 1.234,50 on another, and how do you pin the output?

level: middleimportance: must knowfreq 56%

answer

  1. which thread setting formats holes
  2. separators belong to a culture
  3. a provider for the whole string
  4. files versus people

basics

~10 s

Interpolation formats each hole with CultureInfo.CurrentCulture when the line runs, so N2 takes that culture's separators. Pin it with string.Create(CultureInfo.InvariantCulture, $"..."), FormattableString.Invariant, or ToString(format, provider).

solid answer

~30 s

A `$"..."` assigned to `string` is built by `DefaultInterpolatedStringHandler`, which formats every hole with `CultureInfo.CurrentCulture` unless it is given a provider, so `N2` uses `,`/`.` on en-US and `.`/`,` on de-DE. Dates move too: the `/` in `yyyy/MM/dd` is the culture's date separator, so de-DE prints `2026.10.26`. For text other programs read, format invariantly: `string.Create(CultureInfo.InvariantCulture, $"{total:N2}")` passes the provider to the handler for every hole, and `FormattableString.Invariant($"...")` or `total.ToString("N2", CultureInfo.InvariantCulture)` do the same job. For text people read, pass the customer's culture explicitly rather than trusting the server's. Note that `var s = $"..."` is a plain `string`, already formatted.

code

csharp · 25 lines
csharp
using System.Globalization;

decimal amount = 1234.5m;
var issued = new DateTime(2026, 10, 26);

foreach (var name in new[] { "en-US", "de-DE", "fr-FR" })
{
    CultureInfo.CurrentCulture = new CultureInfo(name);
    Console.WriteLine($"{name}: {amount:N2} | {issued:yyyy/MM/dd} | {issued:d} | {0.5:P0}");
}

CultureInfo.CurrentCulture = new CultureInfo("de-DE");
string a = string.Create(CultureInfo.InvariantCulture, $"{amount:N2} {issued:yyyy/MM/dd}");
string b = FormattableString.Invariant($"{amount:N2} {issued:yyyy/MM/dd}");
string c = amount.ToString("N2", CultureInfo.InvariantCulture);
string d = string.Format(CultureInfo.InvariantCulture, "{0:N2}", amount);
Console.WriteLine($"invariant: {a} | {b} | {c} | {d}");

FormattableString fs = $"{amount:N2} on {issued:d}";
Console.WriteLine(fs.Format);
Console.WriteLine(fs.ArgumentCount);
Console.WriteLine(fs.ToString(new CultureInfo("en-US")));
Console.WriteLine(fs);  // FormattableString.ToString() -> current culture
var inferred = $"{amount:N2}";
Console.WriteLine(inferred.GetType().Name);

go deeper

for a junior

Recall that interpolation uses the current culture and name one way to format invariantly, such as string.Create with CultureInfo.InvariantCulture.

for a middle

Explain why the separators change, that the slash in a custom date pattern is the culture's separator, and how FormattableString keeps the format and arguments for later.

for a senior

Diagnose a file other systems fail to parse after a deploy to another region, and show the fix: invariant formatting for machine-read output, explicit customer culture for documents.

for a principal

Decide whether the team enables CA1305 and which culture each output channel uses, accepting the noise of explicit providers in exchange for output that does not depend on server settings.

## Where the culture comes from When an interpolated string's type is `string` and it has non-string holes, the compiler usually builds it with `System.Runtime.CompilerServices.DefaultInterpolatedStringHandler` (C# 10 with .NET 6 and later). Unless someone hands that handler an `IFormatProvider`, it formats every hole with **`CultureInfo.CurrentCulture`**, read on the thread that runs the line, at the moment it runs. The same is true of `ToString("N2")`, `string.Format(string, ...)` and most parsing methods. The current culture comes from the operating system's regional settings, the container image, or code that sets `CultureInfo.CurrentCulture`. That is why an invoice renderer can print `1,234.50` on a developer laptop and `1.234,50` on a server configured for Germany, with no code change. ## What changes from culture to culture The snippet below sets three cultures in turn and prints the same holes. These are the outputs on the .NET 10 SDK (real culture data, not a stub): | Hole | en-US | de-DE | fr-FR | |---|---|---|---| | `{amount:N2}` | `1,234.50` | `1.234,50` | `1 234,50` (narrow no-break space) | | `{issued:yyyy/MM/dd}` | `2026/10/26` | `2026.10.26` | `2026/10/26` | | `{issued:d}` | `10/26/2026` | `26.10.2026` | `26/10/2026` | | `{0.5:P0}` | `50%` | `50 %` | `50 %` | Two things surprise people: - **The `/` in a custom date pattern is not a literal slash.** It is the culture's date separator, so `yyyy/MM/dd` prints dots under de-DE. Quote it (`yyyy'/'MM'/'dd`) or format invariantly when the slash matters. - **Separators are not always ASCII.** fr-FR groups digits with U+202F, so a parser that splits on a normal space fails too. ## Four ways to choose the culture 1. **`string.Create(IFormatProvider, ref DefaultInterpolatedStringHandler)`** (.NET 6+): `string.Create(CultureInfo.InvariantCulture, $"{amount:N2}")`. The provider goes into the handler's constructor, every hole is formatted with it, and no intermediate string is built. 2. **`FormattableString.Invariant($"...")`**: the interpolated string becomes a `FormattableString`, and `Invariant` calls its `ToString(CultureInfo.InvariantCulture)`. 3. **Per value**: `amount.ToString("N2", CultureInfo.InvariantCulture)`, or `string.Format(CultureInfo.InvariantCulture, "{0:N2}", amount)`. 4. **A specific culture for people**: pass `new CultureInfo("de-DE")`, or the customer's stored culture, instead of relying on whatever the server happens to run with. A tempting wrong fix is `string.Format($"{amount:N2}", CultureInfo.InvariantCulture)`. There is no `string.Format` overload that takes an interpolated string handler, so the `$"..."` is formatted first with the current culture, becomes the format string, and the culture lands in an unused `object` argument. ## FormattableString keeps the format and the arguments Converting an interpolated string to `FormattableString` (or `IFormattable`) makes the compiler call `FormattableStringFactory.Create` instead of formatting immediately: - `fs.Format` is the composite format, for example `{0:N2} on {1:d}`, and `fs.ArgumentCount` is 2. - `fs.ToString(provider)` formats later with any culture; plain `fs.ToString()` uses the current culture. - `var s = $"..."` is a **`string`**, not a `FormattableString`: you get deferral only by asking for the type. - If a method has both a `string` and a `FormattableString` overload, a `$"..."` argument binds to the **string** overload, so `Write($"...")` never reaches the other one by accident. ## Which culture is right - **Text read by software** (CSV and JSON files, file names, cache keys, protocol fields, generated source code) uses the **invariant culture**, so another machine can parse it. - **Text read by people** (the invoice itself) uses **the reader's culture**, chosen explicitly per customer, not the server's default. - **Parsing is the mirror image.** `decimal.Parse` also reads with the current culture: under de-DE, `"1,234.50"` throws `FormatException` and `"1234.50"` silently becomes `123450`, because the dot is the group separator there. A file written invariantly must be read back with `CultureInfo.InvariantCulture` too. - **Round-tripping a date** for another program is safer with the culture-neutral `O` or `yyyy-MM-dd` patterns than with `d`, whose order changes per culture. - Analyzer rule **CA1305**, "Specify IFormatProvider", flags calls to methods that have an `IFormatProvider` overload when you call the one without it, such as `ToString("N2")`. It is not enabled by default in .NET 10, so a team that wants it turns it on.

  • Why does {issued:yyyy/MM/dd} print 2026.10.26 when the current culture is de-DE?
    In a custom date and time format string, `/` is the date separator placeholder, replaced by the culture's `DateTimeFormatInfo.DateSeparator`, which is `.` for de-DE. Quote it as `yyyy'/'MM'/'dd` to force a slash, or format with the invariant culture when another program reads the value.
  • If a method has both a string and a FormattableString overload, which one does Write($"Total {total:N2}") call?
    The `string` overload. The interpolated string converts to `string` more naturally, so it is already formatted with the current culture before the method runs. To reach the invariant result, call `FormattableString.Invariant(...)` or `string.Create(CultureInfo.InvariantCulture, ...)` yourself.
  • Does string.Create(CultureInfo.InvariantCulture, $"...") build the string twice?
    No. The `$"..."` argument binds to a `ref DefaultInterpolatedStringHandler` parameter marked with `[InterpolatedStringHandlerArgument("provider")]`, so the compiler passes the culture into the handler's constructor and each hole is formatted with it straight into the handler's buffer. `ToStringAndClear()` then creates the one result string.

  • Interpolated strings always format with the invariant culture.
  • Only numbers depend on culture; date patterns like yyyy/MM/dd print the same everywhere.
  • Assigning $"..." to var gives a FormattableString you can format later.
  • string.Format($"{total:N2}", CultureInfo.InvariantCulture) formats the total invariantly.
  • FormattableString.Invariant changes the thread's current culture for the code after it.