从指数表示法中解析数字
我需要将字符串“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
它是一个浮点数,你必须告诉它:
It is a floating point number, you have to tell it that:
如果您指定
NumberStyles.Float
,它就可以工作:我不完全确定为什么默认情况下不支持这一点 - 默认情况下是使用
NumberStyles.Number
,它使用AllowLeadingWhite、AllowTrailingWhite、AllowLeadingSign、AllowTrailingSign、AllowDecimalPoint 和允许数千种样式。可能与性能有关;我想,指定指数相对很少见。It works if you specify
NumberStyles.Float
: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.除了指定 NumberStyles 之外,我建议您使用 decimal.TryParse 函数,例如:
作为 NumberStyles.Any 的替代方案,如果您确定格式,则可以使用特定的集合。例如:
In addition to specifying the NumberStyles I would recommend that you use the decimal.TryParse function such as:
As an alternative to NumberStyles.Any you could use a specific set if you're certain of your formats. e.g:
请谨慎选择答案:在 Decimal.Parse 中指定 System.Globalization.NumberStyles.Float 存在微妙之处,这可能会导致 System.FormatException em> 因为您的系统可能正在等待用“,”而不是“.”格式化的数字
例如,在法语表示法中,“1.2345E-02”是无效的,您必须先将其转换为“1,2345E-02”。
总之,使用以下内容:
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(String)
的默认NumberStyle
是NumberStyles.Number
,因此,如果您只想添加允许指数的功能,那么您可以执行按位或来包含NumberStyles.AllowExponent
。The default
NumberStyle
fordecimal.Parse(String)
isNumberStyles.Number
, so if you just want to add the functionality to allow exponents, then you can do a bitwise OR to includeNumberStyles.AllowExponent
.我发现,在某些情况下,传入
NumberStyles.Float
会更改处理字符串的规则,并导致与NumberStyles.Number
不同的输出(decimal.Parse
使用的默认规则)。例如,以下代码将在我的计算机中生成
FormatException
:我建议使用输入
NumberStyles.Number | NumberStyles.AllowExponent
,因为这将允许指数数字,并且仍将根据十进制
规则处理字符串。要回答发帖者的问题,正确答案应该是:
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 fromNumberStyles.Number
(the default rules used bydecimal.Parse
).For example, the following code will generate a
FormatException
in my machine:I'd recommend using the input
NumberStyles.Number | NumberStyles.AllowExponent
, as this will allow exponential numbers and will still process the string under thedecimal
rules.To answer the poster's question, the right answer should instead be:
关于使用 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.
您不需要替换点(分别是逗号),只需指定输入 IFormatProvider:
You don't need to replace the dots (respectively the commas) just specify the input IFormatProvider:
如果您想检查并转换指数值,请使用此
希望这对某人有帮助。
If you want to check and convert the exponent value use this
Hope this helps someone.