34

After search in google, using below code still can not be compiled:

decimal h = Convert.ToDecimal("2.09550901805872E-05");   

decimal h2 = Decimal.Parse(
  "2.09550901805872E-05", 
   System.Globalization.NumberStyles.AllowExponent);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
ControlPoly
  • 717
  • 2
  • 10
  • 21
  • Do you need to _convert_ from a string? Couldn't you just enter the literal `2.09550901805872E-05M`? – Jeff Mercado Jul 11 '13 at 07:42
  • only tryparse can be compiled – ControlPoly Jul 11 '13 at 07:48
  • this compiles just fine for me. you get a runtime exception though. – FalcoGer Jun 19 '19 at 10:23
  • @ControlPoly that is wrong. TryParse will not throw an exception, while Parse may if it fails. Just because something doesn't throw an exception, doesn't mean it's correct. You should fix the problem, i.e. why is the exception thrown (bad format), not the symptom (the exception). Since the format is correct, the correct solution is to make Decimal.Parse accept the format via the correct flags and culture setting. – FalcoGer Jun 19 '19 at 10:26
  • Does this answer your question? [Parse a Number from Exponential Notation](https://stackoverflow.com/questions/3879463/parse-a-number-from-exponential-notation) – Jim G. Nov 10 '22 at 16:38

6 Answers6

52

You have to add NumberStyles.AllowDecimalPoint too:

Decimal.Parse("2.09550901805872E-05", NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);

MSDN is clear about that:

Indicates that the numeric string can be in exponential notation. The AllowExponent flag allows the parsed string to contain an exponent that begins with the "E" or "e" character and that is followed by an optional positive or negative sign and an integer. In other words, it successfully parses strings in the form nnnExx, nnnE+xx, and nnnE-xx. It does not allow a decimal separator or sign in the significand or mantissa; to allow these elements in the string to be parsed, use the AllowDecimalPoint and AllowLeadingSign flags, or use a composite style that includes these individual flags.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
9

Since decimal separator ("." in your string) can vary from culture to culture it's safier to use InvariantCulture. Do not forget to allow this decimal separator (NumberStyles.Float)

  decimal h = Decimal.Parse(
    "2.09550901805872E-05", 
     NumberStyles.Float | NumberStyles.AllowExponent,
     CultureInfo.InvariantCulture);

Perharps, more convenient code is when we use NumberStyles.Any:

  decimal h = Decimal.Parse(
    "2.09550901805872E-05", 
     NumberStyles.Any, 
     CultureInfo.InvariantCulture);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
7

use System.Globalization.NumberStyles.Any

decimal h2 = Decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any);
Damith
  • 62,401
  • 13
  • 102
  • 153
  • If you want to use TryParse: Decimal.TryParse("2.005E01", System.Globalization.NumberStyles.Any, null, out h2); – Ekus Jul 11 '18 at 18:12
2
decimal h = Convert.ToDecimal("2.09550901805872E-05");   
decimal h2 = decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any)
shafi7468
  • 323
  • 1
  • 3
  • 15
2

This thread was very helpful to me. For the benefit of others, here is complete code:

var scientificNotationText = someSourceFile;
// FileTimes are based solely on nanoseconds.
long fileTime = 0;
long.TryParse(scientificNotationText, NumberStyles.Any, CultureInfo.InvariantCulture, 
out fileTime);
var dateModified = DateTime.FromFileTime(fileTime);
j2associates
  • 1,115
  • 10
  • 19
  • Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jul 01 '22 at 04:18
-2
Decimal h2 = 0;
Decimal.TryParse("2.005E01", out h2);
Satpal
  • 132,252
  • 13
  • 159
  • 168
ControlPoly
  • 717
  • 2
  • 10
  • 21