I have a simple C# Web console application that utilises the HTML Agility Pack to retrieve data from a given website. My code takes a value from a form field and attempts to reconstruct it into inches, the code works! however I was wondering if there was a more correct, simpler way of achieving my goal?
string MetricName = "686 x 1981mm, 35mm Thick: £143.01";
var priceGrab = MetricName.Substring(MetricName.LastIndexOf('£') + 1);
// 25.4 is what we divide the MM value by to get the inch value
const double imp = 25.4;
Match firstMeasurements = Regex.Match(MetricName, @"\d+");
Match secondMeasurements = Regex.Match(MetricName, @"x([^,]*)");
Match thicknessMeasurements = Regex.Match(MetricName, @",([^mm]*)");
string firstM = firstMeasurements.Value;
//Convert MM to Inches
double first = double.Parse(firstM) / imp;
string secondM = secondMeasurements.Value;
Match secondFixed = Regex.Match(secondM, @"\d+");
string secondM1 = secondFixed.Value;
//Convert MM to Inches
double second = double.Parse(secondM1) / imp;
string thicknessM = thicknessMeasurements.Groups[1].Value;
Match thirdFixed = Regex.Match(thicknessM, @"\d+");
//Convert MM to Inches
double three = double.Parse(thicknessM) / imp;
ImperialVariant = string.Format("{0} x {1}\", {2}\" Thick: £{3}",first.ToString("00"), second.ToString("00"), three.ToString("0.00"), priceGrab);
return ImperialVariant;
ImperialVariant will then equal to:
30 x 78", 1.38" Thick: £143.01
My program raises the following exception for an unknown reason.
at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Double.Parse(String s)
Is anybody able to provide some feedback on this??
Thank you