Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change NumberDecimalSeparator to a dot in C# interpolated strings for non-english Windows

I have prepared a C# fiddle for my question, however it does not really show the actual problem and correctly uses dots as decimal separator in the interpolated double values (UPDATED with Tim’s trick, thank you):

using System;
using System.Linq;

public class Program
{
    readonly static (double lng, double lat)[] Locations = 
    {
        (10.757938, 52.437444),
        (10.764379, 52.437314),
        (10.770562, 52.439067),
        (10.773268, 52.436633),
    };
    
    public static void Main()
    {
        string lngLats = Locations.Aggregate(string.Empty, (str, next) => str + $"{next.Item1:F6},{next.Item2:F6};");
        Console.WriteLine($"lngLats: {lngLats}\n");
        
        double x = 12.3456;
        Console.WriteLine($"A double value: {x}\n");
    }
}

However, when I run the same code on my Windows 10 Enterprise 22H2 computer, (set to English language, but German region) using Visual Studio 2022, then the decimal separator is a comma:

screenshot VS 2022

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My question is: how to ensure that the decimal separator character is always a dot in the interpolated strings?

>Solution :

String interpolation uses the current culture. So you need a workaround with double.ToString:

double x = 12.3456;
string result = x.ToString(System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine($"A double value: {result}\n");

Your modified fiddle: https://dotnetfiddle.net/BTaONq

So string interpolation doesn’t support a different culture. You could use string.Format:

Console.WriteLine(string.Format(System.Globalization.CultureInfo.InvariantCulture, "A double value: {0}\n", x));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading