【问题标题】:C# string to float?C# 字符串浮动?
【发布时间】:2010-12-23 02:30:09
【问题描述】:

我这里有这段代码:

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

我的问题是这行代码

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

它被转换为int,我如何将它转换为float?

【问题讨论】:

  • float[,] MyArray = new float[5, 5]; MyArray[i, j] = Convert.ToSingle(sites[j]);

标签: c#


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    MyArray[i, j] = Convert.ToSingle(sites[j]);
    

    【讨论】:

      【解决方案3】:

      【讨论】:

      • 使用 Convert.ToDouble,我期待有一个 Convert.ToFloat。不过这是有道理的。
      猜你喜欢
      • 1970-01-01
      • 2012-12-19
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      • 2022-01-21
      相关资源
      最近更新 更多