Parse strings to double with comma and point

c#

Solution

You want to treat dot (`.`) like comma (`,`). So, replace

if (double.TryParse(values[i, j], out tmp))

with

if (double.TryParse(values[i, j].Replace('.', ','), out tmp))

Problem

I am trying to write a function which basically converts an array of strings to an array of strings where all the doubles in the array are rounded to the number of decimalplaces i set. There can also be strings in the array which are no double values at all. ``` string[,] values = new string[1, 3]; values[0, 0] = "hello"; values[0, 1] = "0.123"; values[0, 2] = "0,123"; int decimalPlaces = 2; double tmp; string format = "F" + decimalPlaces.ToString(); IFormatProvider provider = CultureInfo.InvariantCulture; for (int i = 0; i < values.GetLength(0); i++) { for (int j = 0; j < values.GetLength(1); j++) { if (double.TryParse(values[i, j], out tmp)) { values[i, j] = tmp.ToString(format, provider); } } } Console.ReadLine(); ``` The result has to be: "hello" , "0.12", "0.12" but it is "hello", "123.00", "0.12" will treat the comma in the wrong way. Does anyone have a simple and efficient solution for this?

Original source

Related problems