.NET Convert.ToDecimal() and Scientific Notation

I ran into a problem the other day with .NET’s Convert.ToDecimal() , it turns out that it doesn’t handle scientific Notation! Pass it something like “8E-5” and the poor thing just throws an exception…

 

The software in which the problem surfaced was developed as a web application that must deal with numbers read from an Excel chart and so the formats encountered are many and varied!

 

This post points out that you can use this instead:

 


Decimal.Parse(val, System.Globalization.NumberStyles.AllowExponent);

 

But just to be safe I am now using:

 


Decimal.Parse(val, System.Globalization.NumberStyles.Any);

 

And it does work for Exponents, hopefully along with everything else!