I\'m new in C# and need to read float values (x, y, z) from file.
It looks like:
0 -0.01 -0.002
0.000833333333333 -0.01
It seems your culture uses comma as the decimal separator. Try parsing it with InvariantCulture
var value = float.Parse(tempLine.Substring(begin, end), CultureInfo.InvariantCulture);
In addition to this, the way you parsing the lines is more complicated than it should. You can just split the line instead of trying to deal with indices:
foreach(var str in tempLine.Split())
{
float value = float.Parse(str, CultureInfo.InvariantCulture);
}
float.Parse(string) method uses your current culture settings by default. Looks like your CurrentCulture's NumberDecimalSeparator property is , not .
That's why you get FormatException in your "0.54" example.
As a solution, you can use a culture have . as a NumberDecimalSeparator like InvariantCulture as a second parameter in Parse method, or you can .Clone() your CurrentCulture and set it's NumberDecimalSeparator property to .
float number = float.Parse("0.54", CultureInfo.InvariantCulture);
or
var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ".";
float number = float.Parse("0.54", culture);