C#字符串浮动?

Bra*_*ble 9 c#

我在这里有一些代码:

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();
     }
Run Code Online (Sandbox Code Playgroud)

我的问题是这行代码

MyArray[i, j] = Convert.ToInt32(sites[j]);
Run Code Online (Sandbox Code Playgroud)

它被转换为int,如何将其转换为浮点数?

Mat*_*att 33

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


Soa*_*Box 9

MyArray[i, j] = Convert.ToSingle(sites[j]);
Run Code Online (Sandbox Code Playgroud)