2

I have tried this two codes:

1)

Decimal h2 = 0;
Decimal.TryParse("-8.13E-06", out h2);

2)

Decimal.Parse(Convert.ToString(used[row, column].Value), NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint); 

But It is not working for -8.13E-06 Value.

Any other better option to convert Scientific Notation to Decimal?

Thanks in Advance.

user1666620
  • 4,800
  • 18
  • 27

2 Answers2

4

If your culture uses "." as separator:

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

Or you can specify the InvariantCulture:

decimal d = Decimal.Parse("-8.13E-06", System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture);

or as in your exapmple:

Decimal h2 = 0;
Decimal.TryParse("-8.13E-06", NumberStyles.Float, CultureInfo.InvariantCulture, out h2);
Roman
  • 11,966
  • 10
  • 38
  • 47
  • `decimal d = Decimal.Parse("-8.13E-06", System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture);` It worked well Thank you so much. –  Apr 25 '16 at 14:37
1

AllowExponent wont work as per this: https://msdn.microsoft.com/en-us/library/system.globalization.numberstyles(v=vs.110).aspx

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.

System.Globalization.NumberStyles.Float is such a composite style

tolanj
  • 3,651
  • 16
  • 30