Double.Parse 用于本机语言支持
我有一个德语值 20,55
。
当我将区域设置更改为美国时,运行此代码后该值显示为 2055.0
:
double d = Double.Parse("20,55",CultureInfo.CurrentCUCulture);
这是为什么?
I have a value 20,55
which is in German language.
When I change the regional settings to US the value gets displayed as 2055.0
after running this code:
double d = Double.Parse("20,55",CultureInfo.CurrentCUCulture);
Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为在美国
,
是千位分隔符并且被忽略了?就像在德国(和欧洲大陆)中.
是千位分隔符一样,它会被忽略吗?在德国你写:1.000,55。在美国,您可以写:1,000.55(如果前面有一个数字,有些思想流派会删除
,
。所以 1000.55 和 11,000.55)。现在,如果您想确保您的程序使用
de-DE
文化,请在各处作为参数传递Because in US the
,
is the thousand separator and it's ignored? In the same way that in Germany (and continental Europe) the.
is the thousand separator and it's ignored?In Germany you write: 1.000,55. In USA you write: 1,000.55 (and there are some schools of thought that remove the
,
if there is a single digit before. So 1000.55 and 11,000.55).Now, if you want to be sure your program is using the
de-DE
culture, pass as a parameter everywhere在美国,小数点分隔符是
.
,而不是,
。如果您尝试解析20,55
,它(显然)会忽略逗号作为标准三元组分隔符(例如1,234,567.89
),即使没有三位数字在所有这些中。如果要解析欧洲风格的数字,则需要使用欧洲风格的文化设置来解析它。
In the US, the decimal separator is
.
, not,
. If you attempt to parse20,55
, it will (apparently) ignore the commas as standard triplet separators (such as in1,234,567.89
), even though there aren't three digits in all of them.If you want to parse a Euro-style number, you need to parse it with a Euro-style culture setting.
这是因为美国十进制分隔符是
.
而不是,
。如果您想避免该问题,无论用户的区域设置如何,您都可以编写一个扩展方法:
并在使用您的号码时调用此方法:
It's because the US decimal separator is the
.
and not the,
.If you want to avoid that problem no matter the regional settings of the user, you could write a extension method:
And call this method when working with your number: