为 ASP.NET 应用程序中现有的 .NET CultureInfo 创建 NumberFormat
我希望在整个应用程序中以一致的方式格式化我的数字,无论选择哪种文化。事实上,即使对于我们正在使用的基本文化来说,它也是“非标准的”。
我想将“{1500.50:c}”格式设置为:“1500.50”,但我的文化“nl-NL”的标准是:“€ 1.500,00”。由于它是一个网络应用程序,我们没有注册自定义区域性的用户权限,因此我们正在寻找运行时解决方案。
我们想要一个“一劳永逸”的解决方案。不是具有静态(扩展)方法的 Util 类,而是应用程序范围的解决方案,因此我们可以继续使用标准 .ToString("c") 或 ToString("N") 逻辑,这将遵循我们的自定义规则。这将改变文化的 .NumberFormat,但是如何改变呢?一切似乎都是只读的。
谢谢。
I want to format my numbers throughout the application in a consistent way, no matter what culture is chosen. In fact, it's "non-standard" even for the basic culture that we're using.
I want to format "{1500.50:c}" as: '1500.50', but the standard for my culture 'nl-NL', is: '€ 1.500,00'. We don't have the user-rights, since it's a webapplication, to register custom cultures, therefore we're looking for a runtime solution.
We want a "set and forget" solution. Not a Util class with static (extension) methods, but an application wide solution, so we can continue to use the standard .ToString("c"), or ToString("N") logic, which would follow our custom rules. This would be to alter the .NumberFormat of the Culture, but how? Everything seems to be readonly.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将创建一个基类,在该基类上派生所有页面,并设置您想要的文化参数,如下所示:
或者您可以在现有文化的基础上构建您的文化:
I would create a base class on which all your pages are derived and set the parameters you want for the culture there like so:
Or you could build your culture off an existing one:
如果您确实希望无论选择哪种区域性都以一致的方式格式化数字,则应该使用特定的格式模式(“#.##”)以及 InvariantCulture(如果固定区域性没有值)如果您想要数字格式属性,您可以为此目的创建自己的“不变”区域性,设置线程的当前区域性可能会产生其他意想不到的后果,因为默认情况下该区域性将用于所有格式化和解析其中一些可能超出您的范围。 顺便说
一句,您不必使用 CreateSpecificCulture;您可以直接创建一个 CultureInfo:
If you really want to format your numbers in a consistent way no matter what culture is chosen, you should either use a specific format pattern ("#.##") along with the InvariantCulture (if the invariant culture doesn't have the values you want for number format properties, you can create your own "Invariant" culture for this purpose. Setting the thread's current culture can have other unintended consequences as this culture will be used by default for all formatting and parsing some of which may be outside your control.
By the way, you don't have to use CreateSpecificCulture; you can just create a CultureInfo directly:
在将一些“旧版”.NET 站点和服务从一台 Web 服务器迁移到另一台 Web 服务器时,我遇到了类似的情况。
我最终编写了一个 IIS 模块 来覆盖主机货币、数字和日期/时间格式(规定)由主机服务器的区域设置),无需编辑代码。最初,它不支持全局覆盖(无论文化如何都应用特定格式),但这似乎是一个很大的增强,因此已添加。
它允许自定义货币符号、小数分隔符、数字组分隔符、长日期格式、短日期格式...基本上可以通过 NumberFormatInfo 和 DateTimeFormatInfo .NET 中的对象。全部通过 web.config 文件完成(即:无需更改代码)。
希望这可以节省一些人的时间和/或挫败感。
I came across something similar when migrating some "legacy" .NET sites and services from one web server to another.
I ended up writing an IIS module to enable overriding the hosts currency, number and date/time formats (dictated by the regional settings of the host server) without editing code. Initially, it did not support a global override (apply specific format regardless of culture) but this seemed like a great enhancement so has been added.
It enables customization of currency symbols, decimal separators, number group separators, long date format, short date format... basically all the properties that can be explicitly set via the NumberFormatInfo and DateTimeFormatInfo objects in .NET. All done via the web.config file (ie: no code changes required).
Hoping this might save somebody some time and/or frustration.