How to parse a string and convert it to double based on a format string?
<p>I'm currently building a custom validation control where I try to parse a string of a number in a double similar to the <code>DateTime.ParseExact</code> function. </p>

<p>Here is what I try to archive: </p>

<pre><code>Console.WriteLine(Double.ParseExact("12.00", "0.00", CultureInfo.InvariantCulture).ToString()); // should print "12"
Console.WriteLine(Double.ParseExact("asd", "0.00", CultureInfo.InvariantCulture).ToString()); // should throw FormatException
Console.WriteLine(Double.ParseExact("12.000", "0.00", CultureInfo.InvariantCulture).ToString()); // should throw FormatException, because it has to many decimal digits
</code></pre>

<p>I know I could simply parse it without format string, but I want some stricter rules to apply here. The format string can a <a href="https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx" rel="nofollow">standard format string</a> or <a href="https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx" rel="nofollow">custom format string</a>, thus it can be different. </p>

<p>Of course, this method above does not exist. Here is an existing method on the <code>DateTime</code> class that does the same, but with <code>DateTime</code>s: </p>

<pre><code>DateTime.ParseExact("2016-09-06T11:57:55.335", "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
</code></pre>

<p>How can I do the same for <code>Double</code>?</p>

<hr>

<p>By the way ...</p>

<p>the reason for that is I'm building a custom asp.net webforms <code>RangeValidator</code> I inherited from <code>BaseValidator</code>. I want to make this control be compatible with <code>DateTime</code>, too, and it has to use a custom format string and uses the <code>InvariantCulture</code> to make it fully compatible with new HTML5 controls (<code>[type=numeric]</code> and <code>[type=datetime]</code>). </p>

<p>It would be great the numeric validation part of the control acts similar to the DateTime validation part. Also, if someone enters a wrong string representation of the number, it would be nice to decline the request from the validator.</p>