CKTIS / C#
FormattableString.Invariant — invariant-culture interpolation
FormattableString.Invariant formats the arguments of an interpolated string using the invariant culture and returns a string. It produces numeric text independent of the current culture while retaining the supplied format specifiers.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
public static string Invariant(FormattableString formattable);FormattableString.Invariant is a static method in the System namespace, available in .NET 10. It accepts a FormattableString and returns a string, formatting arguments using CultureInfo.InvariantCulture rather than the current culture.
Pass an interpolated expression directly, such as FormattableString.Invariant($"{amount:F2}"), or retain it in a FormattableString variable. That object preserves the format and arguments for later formatting. In contrast, var text = $"{amount:F2}" creates a string; you cannot pass it to this method and recover the original arguments.
In the example, F2 requests two decimal places, and the invariant culture selects a decimal point, producing amount=1234.50. The method does not change the current culture. The directive using static System.FormattableString; lets you shorten the call to Invariant(...).
Use this approach for text that requires culture-independent formatting; user interfaces should generally respect the audience's culture. Invariant culture does not guarantee lossless storage: F2 can round values, so choose a format appropriate to the data requirements.
Examples
using System;
class Program
{
static void Main()
{
decimal amount = 1234.5m;
FormattableString template = $"amount={amount:F2}";
Console.WriteLine(FormattableString.Invariant(template));
}
}Result
amount=1234.50
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.