简体   繁体   English

C#中的Double.parse()用于从网页下载的数据

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

The following code, I download information from a website. 以下代码,我从网站上下载信息。 In this example, I want to extract the information and store result in the double variable called myInfo. 在此示例中,我想提取信息并将结果存储在名为myInfo的双精度变量中。 In this example, the information in a text: "10,000.00". 在此示例中,信息以文本形式:“ 10,000.00”。 I need to convert this into a double and store the information in the variable myInfo. 我需要将其转换为双精度并将信息存储在变量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);
                }
            }
        }
    }
}

With the code above, I get an error: 使用上面的代码,我得到一个错误:

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

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

So there is a format problem. 因此存在格式问题。 But how can I fix it? 但是我该如何解决呢? I have also tried using: 我也尝试过使用:

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

but without success. 但没有成功。 I would like it to be flexible enough to work with US format and European format as well. 我希望它足够灵活以与美国格式和欧洲格式一起使用。 Thank you. 谢谢。

Try this: 尝试这个:

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

If you want to support multiple formats you could use a double.TryParse with different cultures: 如果要支持多种格式,可以使用具有不同区域性的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