从指数表示法解析数字

时间:2010-10-07 07:18:43

标签: c# .net number-formatting exponent

我需要将字符串“1.2345E-02”(以指数表示法表示的数字)解析为十进制数据类型,但Decimal.Parse("1.2345E-02")只会抛出错误

10 个答案:

答案 0 :(得分:153)

这是一个浮点数,你必须告诉它:

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

答案 1 :(得分:44)

如果您指定NumberStyles.Float

,则有效
decimal x = decimal.Parse("1.2345E-02", NumberStyles.Float);
Console.WriteLine(x); // Prints 0.012345

我不完全确定为什么默认情况下不支持 - 默认情况下使用NumberStyles.Number,它使用AllowLeadingWhite,AllowTrailingWhite,AllowLeadingSign,AllowTrailingSign,AllowDecimalPoint和AllowThousands样式。可能与性能有关;我想,指定一个指数相对很少见。

答案 2 :(得分:31)

除了指定NumberStyles外,我还建议您使用 decimal.TryParse 函数,例如:

decimal result;
if( !decimal.TryParse("1.2345E-02", NumberStyles.Any, CultureInfo.InvariantCulture, out result) )
{
     // do something in case it fails?
}

作为NumberStyles.Any的替代方案,如果您确定自己的格式,可以使用特定的集合。例如:

NumberStyles.AllowExponent | NumberStyles.Float

答案 3 :(得分:11)

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

答案 4 :(得分:7)

对所选答案保持谨慎:在 Decimal.Parse 中指定了 System.Globalization.NumberStyles.Float ,这可能会导致系统。 FormatException 因为您的系统可能正在等待使用','而不是'。'格式化的数字。

例如,在法语表示法中,“1.2345E-02”无效,您必须先将其转换为“1,2345E-02”。

总之,使用以下内容:

Decimal.Parse(valueString.Replace('.',','), System.Globalization.NumberStyles.Float);

答案 5 :(得分:3)

我发现在某些情况下,传入NumberStyles.Float会更改处理字符串的规则,并导致NumberStyles.Number的不同输出(decimal.Parse使用的默认规则{1}})。

例如,以下代码将在我的机器中生成FormatException

CultureInfo culture = new CultureInfo("");
culture.NumberFormat.NumberDecimalDigits = 2;
culture.NumberFormat.NumberDecimalSeparator = ".";
culture.NumberFormat.NumberGroupSeparator = ",";
Decimal.Parse("1,234.5", NumberStyles.Float, culture); // FormatException thrown here

我建议使用输入NumberStyles.Number | NumberStyles.AllowExponent,因为这将允许指数数字,并仍然会处理decimal规则下的字符串。

CultureInfo culture = new CultureInfo("");
culture.NumberFormat.NumberDecimalDigits = 2;
culture.NumberFormat.NumberDecimalSeparator = ".";
culture.NumberFormat.NumberGroupSeparator = ",";
Decimal.Parse("1,234.5",NumberStyles.Number | NumberStyles.AllowExponent, culture); // Does not generate a FormatException

要回答海报的问题,正确的答案应该是:

decimal x = decimal.Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent);
Console.WriteLine(x);

答案 6 :(得分:3)

NumberStyle 的默认 decimal.Parse(String)NumberStyles.Number,因此如果您只想添加允许指数的功能,那么您可以执行按位 OR 以包含 NumberStyles.AllowExponent .

decimal d = decimal
    .Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent);

答案 7 :(得分:1)

关于使用NumberStyles.Any:

的警告

“6.33E + 03”按预期转换为6330。在德语中,小数点用逗号表示,但6,33E + 03转换为633000!这对我的客户来说是一个问题,因为生成数据的文化是未知的,可能与运行数据的文化不同。在我的情况下,我总是有科学记数法,所以我总是可以在解析之前将逗号替换为小数点,但是如果你正在使用任意数字,就像1,234,567那样格式化的数字,那么这种方法不起作用。

答案 8 :(得分:0)

您无需替换点(分别用逗号),只需指定输入IFormatProvider:

float d = Single.Parse("1.27315", System.Globalization.NumberStyles.Float, new CultureInfo("en-US"));
float d = Single.Parse("1,27315", System.Globalization.NumberStyles.Float, new CultureInfo("de-DE"));

答案 9 :(得分:0)

如果要检查并转换指数值,请使用此

string val = "1.2345E-02";
double dummy;
bool hasExponential = (val.Contains("E") || val.Contains("e")) && double.TryParse(val, out dummy);
if (hasExponential)
{
    decimal d = decimal.Parse(val, NumberStyles.Float);
}

希望这对某人有帮助。