A receipt test can pass on your laptop and fail on another machine because the formatter silently reads the current culture. Make that dependency visible: accept an IFormatProvider, pass it to the formatting operation, and choose the provider at the call site.
This tutorial uses a three-line receipt with a decimal price, an integer quantity and a date. A complete .NET 10 console program first shows the culture-dependent output, then checks the refactored formatter under en-US, de-DE, fr-FR and ja-JP. It uses only the base class library and was compiled and run in an isolated Linux SDK container.
Separate the format from the culture
A format string describes the requested representation. A provider supplies conventions such as separators and date patterns. Omitting the provider lets formatting use the current culture. A CultureInfo can supply both numeric and date formatting information through IFormatProvider. See Microsoft’s formatting overview.
| Field | Format | Purpose |
|---|---|---|
| Price | N2 |
Number with grouping and two fractional digits |
| Quantity | N0 |
Number with grouping and no fractional digits |
| Date | d |
The provider’s short-date pattern |
The numeric precision affects the displayed string, not the stored value. A price with extra fractional digits can be rounded for display; this example deliberately uses values that already fit two places. The price has no currency symbol: choosing punctuation does not establish a currency or convert an amount. See the standard numeric format reference.
Run the complete before-and-after test
With the .NET 10 SDK installed, create a console project. Replace its entire Program.cs with the program below, then run it. No extra packages are needed.
dotnet new console --framework net10.0 --name ReceiptCulture
cd ReceiptCulture
dotnet run
The program needs the four named cultures to be available. It reports missing culture data to stderr and returns exit code 2 if a culture cannot be created. Run this harness sequentially: it temporarily changes CurrentCulture to exercise the dependency.
using System;
using System.Globalization;
using System.Linq;
using System.Text;
internal static class Program
{
private static int Main()
{
ReceiptLine[] lines =
{
new("Paper", 1234.50m, 1200, new DateTime(2026, 11, 16)),
new("Pen", 2.75m, 2, new DateTime(2026, 1, 5)),
new("Sample", 0m, 0, new DateTime(2026, 12, 31))
};
string[] cultures = { "en-US", "de-DE", "fr-FR", "ja-JP" };
const string expected =
"Paper: price=1,234.50; quantity=1,200; date=11/16/2026\n" +
"Pen: price=2.75; quantity=2; date=01/05/2026\n" +
"Sample: price=0.00; quantity=0; date=12/31/2026\n";
byte[] expectedBytes = Encoding.UTF8.GetBytes(expected);
CultureInfo original = CultureInfo.CurrentCulture;
bool allPassed = true;
try
{
foreach (string name in cultures)
{
CultureInfo.CurrentCulture = new CultureInfo(name, false);
Console.WriteLine("BEFORE " + name);
foreach (ReceiptLine line in lines)
Console.WriteLine(line.FormatBefore());
}
Console.WriteLine("AFTER invariant receipt");
Console.Write(expected);
foreach (string name in cultures)
{
CultureInfo.CurrentCulture = new CultureInfo(name, false);
string actual = string.Join("\n", lines.Select(
line => line.Format(CultureInfo.InvariantCulture))) + "\n";
bool passed = string.Equals(actual, expected, StringComparison.Ordinal)
&& Encoding.UTF8.GetBytes(actual).SequenceEqual(expectedBytes);
Console.WriteLine((passed ? "PASS " : "FAIL ") + name);
if (!passed)
Console.Error.WriteLine("Unexpected receipt:\n" + actual);
allPassed &= passed;
}
return allPassed ? 0 : 1;
}
catch (CultureNotFoundException)
{
Console.Error.WriteLine("This example requires en-US, de-DE, fr-FR and ja-JP culture data.");
return 2;
}
finally
{
CultureInfo.CurrentCulture = original;
}
}
}
internal sealed record ReceiptLine(string Item, decimal Price, int Quantity, DateTime Date)
{
private const string Pattern = "{0}: price={1:N2}; quantity={2:N0}; date={3:d}";
public string FormatBefore() => string.Format(Pattern, Item, Price, Quantity, Date);
public string Format(IFormatProvider provider)
{
ArgumentNullException.ThrowIfNull(provider);
return string.Format(provider, Pattern, Item, Price, Quantity, Date);
}
}
Read the observed output
This is the exact application stdout asserted in the Linux container. The French grouping character is a narrow no-break space, U+202F. Named-culture output can vary with updates to .NET or the operating system, so treat the before section as an observation from this run. Microsoft documents these environmental differences in the InvariantCulture remarks.
BEFORE en-US
Paper: price=1,234.50; quantity=1,200; date=11/16/2026
Pen: price=2.75; quantity=2; date=1/5/2026
Sample: price=0.00; quantity=0; date=12/31/2026
BEFORE de-DE
Paper: price=1.234,50; quantity=1.200; date=16.11.2026
Pen: price=2,75; quantity=2; date=05.01.2026
Sample: price=0,00; quantity=0; date=31.12.2026
BEFORE fr-FR
Paper: price=1 234,50; quantity=1 200; date=16/11/2026
Pen: price=2,75; quantity=2; date=05/01/2026
Sample: price=0,00; quantity=0; date=31/12/2026
BEFORE ja-JP
Paper: price=1,234.50; quantity=1,200; date=2026/11/16
Pen: price=2.75; quantity=2; date=2026/01/05
Sample: price=0.00; quantity=0; date=2026/12/31
AFTER invariant receipt
Paper: price=1,234.50; quantity=1,200; date=11/16/2026
Pen: price=2.75; quantity=2; date=01/05/2026
Sample: price=0.00; quantity=0; date=12/31/2026
PASS en-US
PASS de-DE
PASS fr-FR
PASS ja-JP
English and Japanese use the same numeric punctuation here, but their dates differ. That makes the date field useful: a decimal-only demonstration would miss that difference. German changes the decimal and group separators, while French uses a comma for the decimal separator and a space for grouping.
What makes the test meaningful?
FormatBefore calls string.Format without a provider. The refactored Format method takes the provider explicitly and forwards it to the same operation. Its null guard makes a missing dependency an error. The method itself never changes the ambient culture.
The expected receipt is written independently as a literal. The test does not ask the formatter to generate its own expected result. For each ambient culture it renders all three rows using CultureInfo.InvariantCulture, joins them with literal line feeds, and compares the complete result with that baseline. Both the ordinal string comparison and the UTF-8 byte comparison must succeed. All four checks passed in the recorded run.
The AFTER invariant receipt section prints the expected baseline once; the four PASS lines report comparisons against independently computed actual receipts. A mismatch prints FAIL and the actual receipt, and makes the process return 1. The finally block restores the original culture even when the method returns early.
The byte check covers the constructed receipt, including its explicit line feeds. It does not compare terminal encodings or every platform’s console line endings. Three rows across four cultures provide a focused regression test, not exhaustive coverage of every value or provider.
Choose the provider where the output policy belongs
For a stable test baseline, pass the invariant culture. Its formatting data is culture-independent and cannot be customized by users, as explained in the InvariantCulture reference. For a receipt intended for a person, pass the culture that your product promises that reader. The same formatter supports either decision.
When adapting an existing method, add the provider parameter, forward it through each formatting call, and update callers to choose their intended culture. Keep changes to CurrentCulture in the test harness. This makes the dependency visible without making the production formatter responsible for ambient formatting policy.
The invariant short date shown here is not an ISO date format, and this display-oriented receipt is not a serialization protocol. Define date, precision and delimiter rules separately if another system must parse the result.
For related work, see C# Station’s string interpolation and report-building tutorial. If your problem starts with numeric text rather than typed values, continue with NumberStyles and culture when parsing numbers.