.NET:是否可以更改 double.ToString() 使用的格式?
我有一些调用 double.ToString()
的第三方代码。我的问题是,默认的 double.ToString() 实现用逗号替换小数点,如下所示:
49.99.ToString() == "49,99"
发生这种情况是因为默认的 double.ToString() 使用CultureInfo.CurrentCulture
而我需要它是 CultureInfo.InvariantCulture
。事实上,请观察:
49.99.ToString(CultureInfo.InvariantCulture) == "49.99"
49.99.ToString(CultureInfo.CurrentCulture) == "49,99"
是否有一种方法可以将当前区域性更改为不变的区域性,以便 double.ToString() 按照我的需要工作?
我想尽可能避免修改第三方代码,所以请不要建议我只使用 ToString(IFormatProvider)
重载而不是默认的 ToString().
谢谢。
I have some third party code, that invokes double.ToString()
. My problem, is that the default double.ToString()
implementation replaces the decimal dot with a comma, like so:
49.99.ToString() == "49,99"
This happens, because the default double.ToString()
uses CultureInfo.CurrentCulture
whereas I need it to be CultureInfo.InvariantCulture
. Indeed, observe:
49.99.ToString(CultureInfo.InvariantCulture) == "49.99"
49.99.ToString(CultureInfo.CurrentCulture) == "49,99"
Is there a way to change the current culture to be the invariant one, so that double.ToString()
work as I need it to?
I would like to avoid as much as possible tinkering with the third party code, so please, do not suggest me just using the ToString(IFormatProvider)
overload instead of the default ToString()
.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置默认使用的当前线程的区域性信息:
如果这是一个 Web 应用程序,您可以设置
culture
和uiCulture
属性a href="http://msdn.microsoft.com/en-us/library/hy4kkhe0.aspx" rel="nofollow">
元素web.config
。You could set the culture info of the current thread which is what is used by default:
and if this is a web application you could set the
culture
anduiCulture
attributes of the<globalization>
element in yourweb.config
.