从字符串到双精度或十进制数转换与指数符号字符串、精度、符号、指数

2023-09-03 00:57:11 作者:帅哥

有没有一种快速的方法来转换成数字与指数符号(例如:0.5e10或-5e20)?为十进制或双倍

更新:我发现C#解析从指数符号一个号码但对我来说,除非我指定了一个文化的例子是行不通的。

解决方法:

 双重考验= double.Parse(1.50E-15,CultureInfo.InvariantCulture);
 

解决方案

如果你的文化使用作为小数点分隔符,只是 double.Parse (1.50E-15)应该工作。

写一个程序,要求由键盘输入一个字符,若为大写字母输出A若为小写字母输出a,

如果你的文化用别的东西(如),或者你要确保你的应用程序的工作原理相同的每台计算机上,你应该使用InvariantCulture:

  double.Parse(0.5e10,CultureInfo.InvariantCulture)
 

Is there a fast way to convert numbers with exponential notation (examples: "0.5e10" or "-5e20") to decimal or double?

Update: I found C# Parse a Number from Exponential Notation but the examples won't work for me unless I specified a culture.

Solution:

double test = double.Parse("1.50E-15", CultureInfo.InvariantCulture);

解决方案

If your culture uses . as the decimal separator, just double.Parse("1.50E-15") should work.

If your culture uses something else (e.g. ,) or you want to make sure your application works the same on every computer, you should use InvariantCulture:

double.Parse("0.5e10", CultureInfo.InvariantCulture)