简体   繁体   English

流读取器将System.FormatException倍增

[英]Stream Reader to double System.FormatException

I want to convert yahoo csv stock prices to a C# double array. 我想将yahoo csv股票价格转换为C#双数组。 This is my code: 这是我的代码:

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

The integer iStockTableRows is the amount of stocks. 整数iStockTableRows是股票数量。 The string sSymbols contains the stock symbols, it can look like this: "AAPL+MSFT" 字符串sSymbols包含股票代号,它看起来像这样:“ AAPL + MSFT”

The CSV file looks like this: CSV文件如下所示:

128.61
544.98

When I run it System.FormatException occurs in this line: 当我运行它时,System.FormatException发生在此行:

dCurrentPrice[iLine] = double.Parse(sr.ReadLine());

First it worked and for some reason the same error occurs again. 首先,它起作用了,由于某种原因,同样的错误再次发生。 When I write sr.ReadLine() it doesn't return anything. 当我编写sr.ReadLine()时,它不返回任何内容。

Try: 尝试:

dCurrentPrice[iLine] = double.Parse(sr.ReadLine(), System.Globalization.CultureInfo.InvariantCulture);

Perhaps you are on a non-US computer, with a different decimal separator. 也许您在非美国计算机上,使用不同的小数点分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM