繁体   English   中英

如何将此科学记数法转换为十进制?

[英]How to convert this scientific notation to decimal?

在谷歌搜索后,使用以下代码仍然无法编译

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

decimal h2 = Decimal.Parse(
  "2.09550901805872E-05", 
   System.Globalization.NumberStyles.AllowExponent);

你也必须添加NumberStyles.AllowDecimalPoint

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

MSDN对此很清楚:

指示数字字符串可以采用指数表示法。 AllowExponent 标志允许解析的字符串包含一个以“E”或“e”字符开头的指数,后跟可选的正号或负号和一个整数。 换句话说,它成功解析了 nnnExx、nnnE+xx 和 nnnE-xx 形式的字符串。 它不允许在有效数或尾数中使用小数分隔符或符号; 要允许解析字符串中的这些元素,请使用 AllowDecimalPoint 和 AllowLeadingSign 标志,或使用包含这些单独标志的复合样式。

使用System.Globalization.NumberStyles.Any

decimal h2 = Decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any);

由于小数点分隔符(字符串中的"." )可能因文化而异因此使用InvariantCulture 不要忘记允许这个小数点分隔符( NumberStyles.Float

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

Perharps,更方便的代码是当我们使用NumberStyles.Any

  decimal h = Decimal.Parse(
    "2.09550901805872E-05", 
     NumberStyles.Any, 
     CultureInfo.InvariantCulture);
Decimal h2 = 0;
Decimal.TryParse("2.005E01", out h2);
decimal h = Convert.ToDecimal("2.09550901805872E-05");   
decimal h2 = decimal.Parse("2.09550901805872E-05", System.Globalization.NumberStyles.Any)

这个线程对我很有帮助。 为了他人的利益,这里是完整的代码:

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM