Culture-aware formatting
Culture affects how numbers and dates appear and how text is parsed. Use the user’s selected culture for human-facing text and the format required by a machine-readable contract for stored or exchanged data. Invariant culture is useful when that contract requires stable culture-independent text.
using System;using System.Globalization;decimal price = 12.5m;Console.WriteLine(price.ToString("F2", CultureInfo.InvariantCulture));var comma = new NumberFormatInfo { NumberDecimalSeparator = "," };Console.WriteLine(price.ToString("F2", comma));The stored decimal is unchanged. The two providers choose different display separators.
Expected output (reasoned, not executed):
12.5012,50Separate values from their presentation
A decimal value is not a string with a fixed decimal separator. ToString("F2", provider) produces two fractional digits according to the provider. CultureInfo describes formatting conventions, including separators. Do not infer the user’s culture solely from country or language; let the application choose according to its requirements.
Parse with a narrow input contract
decimal.TryParse(text, styles, provider, out value) reports whether conversion succeeded. A style such as NumberStyles.AllowDecimalPoint allows a decimal point but not group separators or leading signs. An out argument receives the result; this is introduced more fully with methods later. Use an if branch before using the value and validate business ranges separately.
Avoid environment-dependent tests
A test that relies on current culture may fail on a different machine. Specify the provider and format in both formatting and parsing. Named cultures require suitable globalization data; invariant-only runtime configurations may not support creating them. The examples below use invariant culture or a customized NumberFormatInfo so they do not depend on a particular regional data table. For localized dates and currencies use the actual selected culture and currency contract.
using System;using System.Globalization;var style = NumberStyles.AllowDecimalPoint;bool valid = decimal.TryParse("12.50", style, CultureInfo.InvariantCulture, out decimal amount);if (valid) Console.WriteLine(amount.ToString("F2", CultureInfo.InvariantCulture));Console.WriteLine(decimal.TryParse("12,50", style, CultureInfo.InvariantCulture, out _));The selected contract accepts the period, not the comma. The discard _ ignores an unneeded result.
Expected output (reasoned, not executed):
12.50Falseusing System;using System.Globalization;var date = new DateOnly(2026, 4, 9);Console.WriteLine(date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));The explicit year-month-day pattern is unambiguous in this contract.
Expected output (reasoned, not executed):
2026-04-09Common mistake and correction
Do not fix a parsing bug by replacing every comma with a period: a comma may be a grouping separator. Use the known input culture and allowed styles, and reject ambiguous input where the contract requires it. Parsing success does not prove a price is nonnegative or within an acceptable range.
Try it yourself
Accept a nonnegative decimal with a period and no grouping, signs or whitespace. Try "3.50", "3,50", "-1", " 3" and "oops"; print Accepted only for valid inputs.
Next: Arrays and bounds. Examples target C# 14 / net10.0. They are static study material; compiler and ByteMind execution have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Culture-aware formatting — knowledge check
Apply the lesson concepts to the following scenarios.
Does changing the formatting provider change the decimal value?
What does TryParse return?
Which style permits a decimal point without grouping?
Why specify invariant culture in a machine contract?
Does a successful parse prove a valid order price?