为 ASP.NET 应用程序中现有的 .NET CultureInfo 创建 NumberFormat

发布于 2024-08-26 07:31:57 字数 365 浏览 7 评论 0原文

我希望在整个应用程序中以一致的方式格式化我的数字,无论选择哪种文化。事实上,即使对于我们正在使用的基本文化来说,它也是“非标准的”。

我想将“{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 技术交流群。

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

发布评论

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

评论(3

日暮斜阳 2024-09-02 07:31:57

我将创建一个基类,在该基类上派生所有页面,并设置您想要的文化参数,如下所示:

public class PageBase : Page
{
    protected override void InitializeCulture()
    {
        var culture = CultureInfo.CreateSpecificCulture( CultureInfo.CurrentCulture.Name );
        culture.NumberFormat.CurrencySymbol = string.Empty;
        culture.NumberFormat.NumberDecimalDigits = 2;
        culture.NumberFormat.NumberDecimalSeparator = ".";
        culture.NumberFormat.NumberGroupSeparator = ",";
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        base.InitializeCulture();
    }
}

或者您可以在现有文化的基础上构建您的文化:

public class PageBase : Page
{
    protected override void InitializeCulture()
    {
        var culture = CultureInfo.CreateSpecificCulture( "en-US" );
        culture.NumberFormat.CurrencySymbol = string.Empty;
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        base.InitializeCulture();
    }
}

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:

public class PageBase : Page
{
    protected override void InitializeCulture()
    {
        var culture = CultureInfo.CreateSpecificCulture( CultureInfo.CurrentCulture.Name );
        culture.NumberFormat.CurrencySymbol = string.Empty;
        culture.NumberFormat.NumberDecimalDigits = 2;
        culture.NumberFormat.NumberDecimalSeparator = ".";
        culture.NumberFormat.NumberGroupSeparator = ",";
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        base.InitializeCulture();
    }
}

Or you could build your culture off an existing one:

public class PageBase : Page
{
    protected override void InitializeCulture()
    {
        var culture = CultureInfo.CreateSpecificCulture( "en-US" );
        culture.NumberFormat.CurrencySymbol = string.Empty;
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        base.InitializeCulture();
    }
}
山田美奈子 2024-09-02 07:31:57

如果您确实希望无论选择哪种区域性都以一致的方式格式化数字,则应该使用特定的格式模式(“#.##”)以及 InvariantCulture(如果固定区域性没有值)如果您想要数字格式属性,您可以为此目的创建自己的“不变”区域性,设置线程的当前区域性可能会产生其他意想不到的后果,因为默认情况下该区域性将用于所有格式化和解析其中一些可能超出您的范围。 顺便说

一句,您不必使用 CreateSpecificCulture;您可以直接创建一个 CultureInfo:

        CultureInfo currentWithOverriddenNumber = new CultureInfo(CultureInfo.CurrentCulture.Name);
        currentWithOverriddenNumber.NumberFormat.CurrencyPositivePattern = 0; // make sure there is no space between symbol and number
        currentWithOverriddenNumber.NumberFormat.CurrencySymbol = ""; // no currency symbol
        currentWithOverriddenNumber.NumberFormat.CurrencyDecimalSeparator = "."; //decimal separator
        currentWithOverriddenNumber.NumberFormat.CurrencyGroupSizes = new int[] { 0 }; //no digit groupings
        currentWithOverriddenNumber.NumberFormat.NumberGroupSizes = new int[] { 0 };
        currentWithOverriddenNumber.NumberFormat.NumberDecimalSeparator = "."; //decimal separator

        Thread.CurrentThread.CurrentCulture = currentWithOverriddenNumber;

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:

        CultureInfo currentWithOverriddenNumber = new CultureInfo(CultureInfo.CurrentCulture.Name);
        currentWithOverriddenNumber.NumberFormat.CurrencyPositivePattern = 0; // make sure there is no space between symbol and number
        currentWithOverriddenNumber.NumberFormat.CurrencySymbol = ""; // no currency symbol
        currentWithOverriddenNumber.NumberFormat.CurrencyDecimalSeparator = "."; //decimal separator
        currentWithOverriddenNumber.NumberFormat.CurrencyGroupSizes = new int[] { 0 }; //no digit groupings
        currentWithOverriddenNumber.NumberFormat.NumberGroupSizes = new int[] { 0 };
        currentWithOverriddenNumber.NumberFormat.NumberDecimalSeparator = "."; //decimal separator

        Thread.CurrentThread.CurrentCulture = currentWithOverriddenNumber;
农村范ル 2024-09-02 07:31:57

在将一些“旧版”.NET 站点和服务从一台 Web 服务器迁移到另一台 Web 服务器时,我遇到了类似的情况。

我最终编写了一个 IIS 模块 来覆盖主机货币、数字和日期/时间格式(规定)由主机服务器的区域设置),无需编辑代码。最初,它不支持全局覆盖(无论文化如何都应用特定格式),但这似乎是一个很大的增强,因此已添加。

它允许自定义货币符号、小数分隔符、数字组分隔符、长日期格式、短日期格式...基本上可以通过 NumberFormatInfoDateTimeFormatInfo .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.

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