如何在 C# 中将正负科学计数法转换为数字?

How to Convert Positive or Negative Scientific Notation to Number in C#?

我试过这两个代码:

1)

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

2)

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

但它不适用于 -8.13E-06 值。

还有其他更好的方法可以将科学记数法转换为十进制吗?

提前致谢。

如果您的文化使用“.”作为分隔符:

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

或者您可以指定 InvariantCulture:

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

或如您的示例所示:

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

AllowExponent 不会按以下方式工作: 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就是这样的复合风格