流 Reader 加倍 System.FormatException
Stream Reader to double System.FormatException
我想将雅虎 csv 股票价格转换为 C# 双精度数组。
这是我的代码:
WebRequest wrPrice = WebRequest.Create("http://finance.yahoo.com/d/quotes.csv?s=" + sSymbols + "&f=a"); //sSymbols zb. "AAPL+GOOG+MSFT"
WebResponse wResp = wrPrice.GetResponse();
StreamReader sr = new StreamReader(wResp.GetResponseStream());
double[] dCurrentPrice = new double[iStockTableRows];
int iLine = 0;
while (sr.Peek() >= 0)
{
dCurrentPrice[iLine] = double.Parse(sr.ReadLine());
iLine++;
}
sr.Close();
整数iStockTableRows是股票数量。
字符串 sSymbols 包含股票代码,它可以是这样的:"AAPL+MSFT"
CSV 文件如下所示:
128.61
544.98
当我 运行 它 System.FormatException 出现在这一行:
dCurrentPrice[iLine] = double.Parse(sr.ReadLine());
首先它起作用了,但由于某种原因再次出现同样的错误。
当我写 sr.ReadLine() 它没有 return 任何东西。
尝试:
dCurrentPrice[iLine] = double.Parse(sr.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);
也许您使用的是非美国计算机,使用不同的小数点分隔符。
我想将雅虎 csv 股票价格转换为 C# 双精度数组。 这是我的代码:
WebRequest wrPrice = WebRequest.Create("http://finance.yahoo.com/d/quotes.csv?s=" + sSymbols + "&f=a"); //sSymbols zb. "AAPL+GOOG+MSFT"
WebResponse wResp = wrPrice.GetResponse();
StreamReader sr = new StreamReader(wResp.GetResponseStream());
double[] dCurrentPrice = new double[iStockTableRows];
int iLine = 0;
while (sr.Peek() >= 0)
{
dCurrentPrice[iLine] = double.Parse(sr.ReadLine());
iLine++;
}
sr.Close();
整数iStockTableRows是股票数量。 字符串 sSymbols 包含股票代码,它可以是这样的:"AAPL+MSFT"
CSV 文件如下所示:
128.61
544.98
当我 运行 它 System.FormatException 出现在这一行:
dCurrentPrice[iLine] = double.Parse(sr.ReadLine());
首先它起作用了,但由于某种原因再次出现同样的错误。 当我写 sr.ReadLine() 它没有 return 任何东西。
尝试:
dCurrentPrice[iLine] = double.Parse(sr.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);
也许您使用的是非美国计算机,使用不同的小数点分隔符。