从指数表示法中解析数字

发布于 2024-09-26 14:33:13 字数 99 浏览 3 评论 0原文

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

I need to parse the string "1.2345E-02" (a number expressed in exponential notation) to a decimal data type, but Decimal.Parse("1.2345E-02") simply throws an error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

生生不灭 2024-10-03 14:33:13

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

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

It is a floating point number, you have to tell it that:

decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
东北女汉子 2024-10-03 14:33:13

如果您指定NumberStyles.Float,它就可以工作

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

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

It works if you specify NumberStyles.Float:

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

I'm not entirely sure why this isn't supported by default - the default is to use NumberStyles.Number, which uses the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles. Possibly it's performance-related; specifying an exponent is relatively rare, I suppose.

手心的海 2024-10-03 14:33:13

除了指定 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

In addition to specifying the NumberStyles I would recommend that you use the decimal.TryParse function such as:

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

As an alternative to NumberStyles.Any you could use a specific set if you're certain of your formats. e.g:

NumberStyles.AllowExponent | NumberStyles.Float
逆光飞翔i 2024-10-03 14:33:13
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
decimal d = Decimal.Parse("1.2345E-02", System.Globalization.NumberStyles.Float);
没有心的人 2024-10-03 14:33:13

请谨慎选择答案:在 Decimal.Parse 中指定 System.Globalization.NumberStyles.Float 存在微妙之处,这可能会导致 System.FormatException em> 因为您的系统可能正在等待用“,”而不是“.”格式化的数字

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

总之,使用以下内容:

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

Be cautious about the selected answer: there is a subtility specifying System.Globalization.NumberStyles.Float in Decimal.Parse which could lead to a System.FormatException because your system might be awaiting a number formated with ',' instead of '.'

For instance, in french notation, "1.2345E-02" is invalid, you have to convert it to "1,2345E-02" first.

In conclusion, use something along the lines of:

Decimal.Parse(valueString.Replace('.',','), System.Globalization.NumberStyles.Float);
热血少△年 2024-10-03 14:33:13

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

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

The default NumberStyle for decimal.Parse(String) is NumberStyles.Number, so if you just want to add the functionality to allow exponents, then you can do a bitwise OR to include NumberStyles.AllowExponent.

decimal d = decimal
    .Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent);
一袭白衣梦中忆 2024-10-03 14:33:13

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

例如,以下代码将在我的计算机中生成 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,因为这将允许指数数字,并且仍将根据十进制规则处理字符串。

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

I've found that passing in NumberStyles.Float, in some cases, changes the rules by which the string is processed and results in a different output from NumberStyles.Number (the default rules used by decimal.Parse).

For example, the following code will generate a FormatException in my machine:

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

I'd recommend using the input NumberStyles.Number | NumberStyles.AllowExponent, as this will allow exponential numbers and will still process the string under the decimal rules.

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

To answer the poster's question, the right answer should instead be:

decimal x = decimal.Parse("1.2345E-02", NumberStyles.Number | NumberStyles.AllowExponent);
Console.WriteLine(x);
顾北清歌寒 2024-10-03 14:33:13

关于使用 NumberStyles.Any 的警告:

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

Warning about using NumberStyles.Any:

"6.33E+03" converts to 6330 as expected. In German, decimal points are represented by commas, but 6,33E+03 converts to 633000! This is a problem for my customers, as the culture that generates the data is not known and may be different than the culture that is operating on the data. In my case, I always have scientific notation, so I can always replace comma to decimal point before parsing, but if you are working with arbitrary numbers, like pretty-formatted numbers like 1,234,567 then that approach doesn't work.

流绪微梦 2024-10-03 14:33:13

您不需要替换点(分别是逗号),只需指定输入 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"));

You don't need to replace the dots (respectively the commas) just specify the input 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"));
扎心 2024-10-03 14:33:13

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

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

希望这对某人有帮助。

If you want to check and convert the exponent value use this

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

Hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文