[英]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]);
Convert.ToSingle method or whole bunch of others . Convert.ToSingle方法或其他一大堆 。
EDIT: 编辑:
Here's an related article: Double.TryParse or Double.Convert - what is faster and more safe? 这是一篇相关的文章:Double.TryParse或Double.Convert - 什么更快更安全? of interest in SO.
对SO感兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.