To parse a number from string given in Exponential Notation necessary is to use following piece of code:
- decimal myDecimalValue = Decimal.Parse("1,11E-04", System.Globalization.NumberStyles.Float);
- Console.WriteLine(myDecimalValue);
If parsing is not possible due to wrong format of string we got an exception. To avoid this situation we can use following piece of code:
- decimal result;
- Decimal.TryParse("1.11E-04", System.Globalization.NumberStyles.Float, null, out result);
In this case, when parsing is not possible - result variable will be set to 0, but not exception will be thrown. Please note in this approach we have to use "out" parameter. TryParse of Decimal returns bool - if parsing was possible or not.