.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!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *