Parse a Number from Exponential Notation

It is a floating point number, you have to tell it that:

decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);

It works if you specify NumberStyles.Float:

decimal x = decimal.Parse("1.2345E-02", NumberStyles.Float);
Console.WriteLine(x); // Prints 0.012345

I'm not entirely sure why this isn't supported by default - the default is to use NumberStyles.Number, which uses the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles. Possibly it's performance-related; specifying an exponent is relatively rare, I suppose.


In addition to specifying the NumberStyles I would recommend that you use the decimal.TryParse function such as:

decimal result;
if( !decimal.TryParse("1.2345E-02", NumberStyles.Any, CultureInfo.InvariantCulture, out result) )
{
     // do something in case it fails?
}

As an alternative to NumberStyles.Any you could use a specific set if you're certain of your formats. e.g:

NumberStyles.AllowExponent | NumberStyles.Float

decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);