簡體   English   中英

C#中的Double.parse()用於從網頁下載的數據

[英]Double.parse() in C# for data downloaded from webpage

以下代碼,我從網站上下載信息。 在此示例中,我想提取信息並將結果存儲在名為myInfo的雙精度變量中。 在此示例中,信息以文本形式:“ 10,000.00”。 我需要將其轉換為雙精度並將信息存儲在變量myInfo中。

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;
using System.Globalization;
using System.Net;
using System.IO;
using HtmlAgilityPack;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://www.eurexchange.com/exchange-en/products/idx/stx/blc/19068!quotesSingleViewOption?callPut=Call&maturityDate=201412";

            var webGet = new HtmlWeb();
            var document = webGet.Load(url);

            var pricesAndQuotesDataTable = (from elem in document.DocumentNode.Descendants()
                    .Where(d =>
                    d.Attributes["class"] != null && d.Attributes["class"].Value == "toggleTitle" &&
                    d.ChildNodes.Any(h => h.InnerText != null && h.InnerText == "Prices/Quotes"))
                                            select elem.Descendants()
                                            .FirstOrDefault(
                                            d => d.Attributes["class"] != null && d.Attributes["class"].Value == "dataTable")).FirstOrDefault();

            if (pricesAndQuotesDataTable != null)
            {
                var dataRows = from elem in pricesAndQuotesDataTable.Descendants()
                               where elem.Name == "tr" && elem.ParentNode.Name == "tbody"
                               select elem;

                foreach (var row in dataRows)
                {
                    var dataColumns = (from col in row.ChildNodes.Where(n => n.Name == "td")
                                       select col).ToList();
                    double myInfo = double.Parse(dataColumns[0].InnerText, NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
                }
            }
        }
    }
}

使用上面的代碼,我得到一個錯誤:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

因此存在格式問題。 但是我該如何解決呢? 我也嘗試過使用:

double myInfo  = Double.Parse(dataColumns[0].InnerText, CultureInfo.InvariantCulture);

但沒有成功。 我希望它足夠靈活以與美國格式和歐洲格式一起使用。 謝謝。

嘗試這個:

    var numberFormatInfo = new NumberFormatInfo();
    numberFormatInfo.NumberDecimalSeparator = ".";
    numberFormatInfo.NumberGroupSeparator = ",";
    double result;
    if (double.TryParse("10,000.00", NumberStyles.Any, numberFormatInfo, out result))
    {
        Console.WriteLine(result);
    }

如果要支持多種格式,可以使用具有不同區域性的double.TryParse

string[] samples = { "10,000.00", "10.000,00" };
CultureInfo deDE = new CultureInfo("de-DE");  // to support your "European format"
NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands;

foreach (string sample in samples)
{
    double value;
    bool parsable = double.TryParse(sample, style, NumberFormatInfo.InvariantInfo, out value);
    if(!parsable)
        parsable = double.TryParse(sample, style, deDE, out value);
    Console.WriteLine(value); // output 10000 with both
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM