私はここにこのコードを持っています:
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に変換するにはどうすればよいですか?
MyArray[i, j] = Convert.ToSingle(sites[j]);
Float.Parse(string)またはDouble.Parse(string)を試してください
Convert.ToSingle メソッドまたは 他のすべての束 。
編集:
ここに 関連記事:Double.TryParseまたはDouble.Convert-何がより速く、より安全ですか? SOに関心があります。