C# float.Parse String

后端 未结 2 1161
轻奢々
轻奢々 2020-12-18 20:40

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

2条回答
  •  时光说笑
    2020-12-18 20:42

    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);
    }
    

提交回复
热议问题