简体   繁体   English

C#字符串浮动?

[英]C# string to float?

I have this bit of code here: 我在这里有一些代码:

int i = 0;

        StreamReader re = File.OpenText("TextFile1.txt");
        string input = null;

        while ((input = re.ReadLine()) != null)
        {
            string[] sites = input.Split(' ');
            for (int j = 0; j < sites.Length; j++)
            {
                MyArray[i, j] = Convert.ToInt32(sites[j]);
            }
            i++;
        }


     for (int a = 0; a < 5; a++)
     {
            for (int j = 0; j < 5; j++)
            {
                Console.Write(MyArray[a, j] + " ");

            }
            Console.WriteLine();
     }

My problem is this line of code 我的问题是这行代码

MyArray[i, j] = Convert.ToInt32(sites[j]);

Its getting converted to an int, how do I convert it to a float? 它被转换为int,如何将其转换为浮点数?

试试float.Parse(string)或Double.Parse(string)

MyArray[i, j] = Convert.ToSingle(sites[j]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM