C#
double
ToString
decimal point
programming

How to change symbol for decimal point in double.ToString?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In .NET, double.ToString() uses culture settings to determine decimal and thousands separators. If your current culture uses comma decimal separators, you may get 12,34 instead of 12.34, or the reverse. This is correct behavior, but it becomes a problem when generating machine-readable output, integrating with external systems, or enforcing locale-specific UI formatting.

The right solution is to format numbers with an explicit CultureInfo (or NumberFormatInfo) instead of relying on ambient thread culture. This keeps output deterministic and avoids parsing bugs across environments.

Core Sections

1. Use built-in culture directly

csharp
1using System;
2using System.Globalization;
3
4double value = 1234.56;
5string us = value.ToString("0.00", CultureInfo.InvariantCulture); // 1234.56
6string fr = value.ToString("0.00", CultureInfo.GetCultureInfo("fr-FR")); // 1234,56

For APIs and files, InvariantCulture is usually safest.

2. Customize decimal separator explicitly

Clone a culture and override NumberDecimalSeparator.

csharp
1var custom = (CultureInfo)CultureInfo.InvariantCulture.Clone();
2custom.NumberFormat.NumberDecimalSeparator = "|";
3
4double v = 98.765;
5string formatted = v.ToString("0.000", custom); // 98|765

This is useful for legacy protocols with unusual formatting requirements.

3. Keep parsing and formatting symmetric

If you format with custom separator, parse with the same provider.

csharp
double parsed = double.Parse("98|765", NumberStyles.Float, custom);

Using mismatched culture providers is a common source of conversion errors.

4. Avoid global culture mutation when possible

You can set thread culture, but it affects wide areas of code.

csharp
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");

Prefer passing IFormatProvider per call for predictable behavior in libraries and services.

5. Separate display formatting from data formatting

UI may require locale formatting, while storage/API requires invariant formatting.

csharp
string ui = amount.ToString("N2", userCulture);
string wire = amount.ToString("G17", CultureInfo.InvariantCulture);

Keeping these concerns separate prevents subtle interoperability bugs.

6. Add tests for culture-sensitive code

csharp
1[Fact]
2public void FormatsDecimalPointWithInvariantCulture()
3{
4    double x = 1.5;
5    var s = x.ToString("0.0", CultureInfo.InvariantCulture);
6    Assert.Equal("1.5", s);
7}

Culture-sensitive tests are especially important in CI environments where system locale can differ.

Common Pitfalls

  • Assuming double.ToString() output is locale-independent by default.
  • Replacing decimal symbols with string .Replace instead of using culture-aware formatting.
  • Formatting with one culture and parsing with another, causing runtime parse exceptions.
  • Changing global thread culture in shared libraries and affecting unrelated code.
  • Using localized numeric formatting for machine-to-machine payloads.

Summary

To change the decimal symbol for double.ToString(), use an explicit culture or customized NumberFormatInfo rather than relying on ambient settings. Keep data and UI formatting separate, and ensure parsing uses the same provider as formatting. With explicit culture handling, numeric output stays predictable across machines, locales, and deployment environments.

A practical way to harden this topic in real projects is to add a small operational checklist and treat it as part of your engineering standard, not a one-off fix. Start by creating one minimal failing case and one passing case that represent real input from production logs. Then automate those checks in CI so regressions are caught before release. Add lightweight instrumentation around the critical branch where this logic runs, and include structured fields that let you filter by version, environment, and error type. This gives you fast feedback when behavior changes after dependency upgrades or refactors.

For long-term maintainability on how to change symbol for decimal point in doubletostring, keep one source of truth for helper logic instead of duplicating variants across services or UI layers. Document assumptions near the code, including data format, edge-case behavior, and expected fallback policy. During code review, verify that example inputs and tests cover empty values, malformed values, and high-volume scenarios. Teams that combine explicit assumptions, repeatable tests, and basic observability typically avoid the same category of bug recurring every quarter.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.