'How to convert string with comma to float in .net?

var withComma = Convert.ToSingle("3,2");
Console.WriteLine($"Converted float from string with comma (3,2): {withComma}");

It returns to the console:

Converted float from string with comma (3,2): 32

But should return:

Converted float from string with comma (3,2): 3.2

How can I convert it to float and make the result to be 3.2?



Solution 1:[1]

https://dotnetfiddle.net/TnQyL9

Console.WriteLine(float.Parse("3,2", System.Globalization.CultureInfo.GetCultureInfo("es-ES")));

prints: 3.2

There is a lot of cultures which have this format. Just pick one you are fine with.

Solution 2:[2]

Finally I'm using this approach

private void convertToFloat(string str) 
{
    float result = float.Parse(str.Replace(',', '.'));
    Console.WriteLine(result);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eocron
Solution 2 SValley_Dev