Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix the Culture-Specific Bug That Breaks Number Parsing for Users Outside the US

- 30.08.26 | 31.08.26 - ErcanOPAK

🔧 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.

— C# Best Practice

Related posts:

C#: Why Pattern Matching Against a Null Object Doesn't Throw, and Why That's Not Always Safe

C#: Why Pattern Matching Against a Null Object Doesn't Throw, and Why That's Not Always Safe

ValueTask for Hot Paths

ValueTask for Hot Paths

C#: Use String Interpolation with Alignment for Formatted Output

C#: Use String Interpolation with Alignment for Formatted Output

Views: 13
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Ajax: Stop CORS Preflight Requests From Doubling Your API’s Load
CSS: Fix Flexbox Items That Refuse to Shrink Below Their Content Size

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com