0
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?

SValley_Dev
  • 638
  • 4
  • 8
  • 1
    Use `float.Parse()` or `float.TryParse()` with the appropriate [CultureInfo](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo). – 41686d6564 stands w. Palestine May 11 '22 at 09:37
  • Is this a European number? Maybe pass along a format provider as the second argument to `Convert.ToSingle(String, IFormatProvider)` or set the _culture_ on the current thread –  May 11 '22 at 09:38
  • Possible duplicate of https://stackoverflow.com/questions/11202673/converting-string-to-float-in-c-sharp/11203062#11203062 – Matthew Watson May 11 '22 at 09:43
  • Does this answer your question? [Converting String To Float in C#](https://stackoverflow.com/questions/11202673/converting-string-to-float-in-c-sharp) – eocron May 11 '22 at 09:47
  • @eocron No, it don't work – SValley_Dev May 11 '22 at 09:49

2 Answers2

0

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.

eocron
  • 6,885
  • 1
  • 21
  • 50
  • But then if I will try with the "3.2" instead of "3,2" it will return 32, I need something that will work for both scenarios – SValley_Dev May 11 '22 at 10:07
  • 1
    You do realize that you have multiple formats: 3,000.1 and 3.000,1 ? You need to know culture beforehand for correct parsing. If you can't distinguish I think you need some custom heuristic to distinguish (replace like in other answer, or assume that last separator is float separator, which comes with pros/cons and hard to debug error) – eocron May 11 '22 at 10:13
  • @SValley_Dev If you're in the situation where you don't know whether "1,000" means one thousand or `1.0`, then your data needs to be sanitised. – Matthew Watson May 11 '22 at 10:13
  • @MatthewWatson I know what it means, but different clients can use different cultures on their machine and I don't know what value will come to me in format with dot or in format with comma – SValley_Dev May 11 '22 at 10:36
  • @SValley_Dev do you have access to client code? You can send culture in http headers if they are not already sent - HttpContext.Request.GetTypedHeaders().AcceptLanguage. Believe me, you are not the first one who deal with multicultural systems and there is already handy ways to manage those. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language – eocron May 11 '22 at 11:18
-1

Finally I'm using this approach

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