.NET:是否可以更改 double.ToString() 使用的格式?

发布于 2024-12-26 17:34:04 字数 618 浏览 3 评论 0原文

我有一些调用 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 技术交流群。

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

发布评论

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

评论(1

不必了 2025-01-02 17:34:04

您可以设置默认使用的当前线程的区域性信息:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

如果这是一个 Web 应用程序,您可以设置 cultureuiCulture 属性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:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

and if this is a web application you could set the culture and uiCulture attributes of the <globalization> element in your web.config.

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