There is an article on Single.TryParse over at MSDN with this example code: http://msdn.microsoft.com/en-us/library/26sxas5t%28v=vs.100%29.aspx
// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Single.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("Unable to parse '{0}'.", value);
Problem is in the article the TryParse returns true
and the string is converted, but when I try it, it's false
. How do I fix this?
UPD: To simplify parsing, these two lines can be used:
NumberStyles style = System.Globalization.NumberStyles.Any;
CultureInfo culture = CultureInfo.InvariantCulture;
This setting allows for negative floats and strings with leading and trailing space characters to be parsed.