简体   繁体   中英

Stream Reader to double System.FormatException

I want to convert yahoo csv stock prices to a C# double array. 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. The string sSymbols contains the stock symbols, it can look like this: "AAPL+MSFT"

The CSV file looks like this:

128.61
544.98

When I run it System.FormatException occurs in this line:

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.

Try:

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

Perhaps you are on a non-US computer, with a different decimal separator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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