Iam 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 -0.002
If Iam trying
float number = float.Parse("0,54"); // it works well, but
float number = float.Parse("0.54"); // gains exepction.
My code for reading values from each line (could be bugged):
int begin = 0;
int end = 0;
for (int i = 0; i < tempLine.Length; i++)
{
if (Char.IsWhiteSpace(tempLine.ElementAt(i)))
{
end = i;
float value = float.Parse(tempLine.Substring(begin, end));
begin = end;
System.Console.WriteLine(value);
}
}
someone could help ?
float.Parse(string)
method uses your current culture settings by default. Looks like yourCurrentCulture
'sNumberDecimalSeparator
property is,
not.
That's why you get
FormatException
in your"0.54"
example.As a solution, you can use a culture have
.
as aNumberDecimalSeparator
likeInvariantCulture
as a second parameter inParse
method, or you can.Clone()
yourCurrentCulture
and set it'sNumberDecimalSeparator
property to.
or
It seems your culture uses
comma
as the decimal separator. Try parsing it withInvariantCulture
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: