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. In this example, the information in a text: "10,000.00". I need to convert this into a double and store the information in the variable 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:
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
}
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.