Hi I'd like to convert string
to float
I try with float.Parse
()
the problem the string format is with some user ##,### and with other ##.### it depends on user CultureInfo
When I convert string to float I have an error because the float format is with ','
so how to manage that?
Asked
Active
Viewed 1,857 times
1

Rad
- 4,403
- 4
- 24
- 25
-
1What is the exact code you're using, and a sample float... This might help: http://stackoverflow.com/questions/6171202/c-sharp-parsing-float-from-string – Sadek Noureddine Oct 19 '13 at 09:52
2 Answers
0
Just follow the example from Double.TryParse Method. Instead of specifying the culture, just ask the system for the current culture.
string userInput = "56,67";
float output;
System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint;
System.Globalization.CultureInfo info = System.Threading.Thread.CurrentThread.CurrentCulture;
if (float.TryParse(userInput, style, info, out output))
{
// ...
}
else
{
// ....
}

Black Frog
- 11,595
- 1
- 35
- 66
0
Use the over load which looks a below
float.Parse("20,30", System.Globalization.NumberStyles.Float,
Thread.CurrentThread.CurrentUICulture);
Or you can use double.parse also as below
double.Parse("30.30", System.Globalization.NumberStyles.Float,
Thread.CurrentThread.CurrentUICulture);

Black Frog
- 11,595
- 1
- 35
- 66

Kumareshan
- 1,311
- 8
- 8