🔧 double.Parse() Works Fine — Until It Doesn’t
Code that parses numbers with double.Parse() or ToString() using the default (current) culture works perfectly on your machine, then throws FormatException or silently produces a wrong value on a server or user’s PC set to a different regional format.
🐞 The Problem
double price = double.Parse("1234.56");
// Works fine on a US-culture machine.
// On a machine set to Turkish, German, or most of Europe,
// the DECIMAL separator is a comma, not a period - so this
// either throws FormatException, or worse, silently parses
// "1234.56" as 123456 if the culture treats '.' as a
// thousands separator instead.
🔍 Why This Happens
double.Parse(string) without a culture argument uses CultureInfo.CurrentCulture by default - whatever regional format is configured on the OS running the code. A number that came from an API, a config file, or a CSV export is almost always meant to be culture-INVARIANT (always uses '.'), but the parsing code doesn't know that unless you tell it explicitly.
✅ The Fix: Be Explicit About Which Culture You Mean
- Machine-readable data (APIs, files, database values, config): always use CultureInfo.InvariantCulture.
- User-facing display (what a person types into a form, what’s shown on screen): use CultureInfo.CurrentCulture, since that’s genuinely locale-dependent by design.
- Never rely on the default overload for either case — always pass the culture explicitly so the intent is visible in the code.
🌍 Correct Version
// Parsing machine/API data - always invariant
double price = double.Parse("1234.56", CultureInfo.InvariantCulture);
// Formatting for a human to read on screen - current culture is right
string display = price.ToString("N2", CultureInfo.CurrentCulture);
// Parsing what a USER typed into your app's UI - current culture,
// because that's the format they actually typed in
double userInput = double.Parse(textBox.Text, CultureInfo.CurrentCulture);
⚠️ Also Watch For
- int.Parse, decimal.Parse, and DateTime.Parse all have the exact same culture-dependent default — audit all of them, not just double.
- JSON serializers typically use invariant culture internally already, so this bug usually hides in hand-written parsing, not in your API layer.
This bug is invisible in every developer’s local testing because everyone’s dev machine happens to share a culture — it only shows up in front of a real user or a server halfway around the world.




