【问题标题】:How can I override the currency formatting for the current culture for an ASP.NET web application?如何覆盖 ASP.NET Web 应用程序当前区域性的货币格式?
【发布时间】:2011-08-26 07:46:50
【问题描述】:

en-ZA 地区的货币小数点和千位分隔符分别是“,”和“”,但常用的分隔符是“.”。对于十进制,加上我的用户想要','作为千位分隔符。我希望在全局范围内设置这些,这样我只需对我的所有货币字段使用{0:C} 格式字符串,而不必进行任何显式的FormatToString 调用。

我希望能够在不更改网络服务器上的文化设置的情况下执行此操作,因为我还需要将货币的小数位设置为零,因为在报告 R100k 及以上的估计值时不需要美分等. 我不想随意将整个文化设置为零位,只为这个应用程序设置一个。

在他对this question 的回答中,Jon Skeet 建议克隆当前的文化和设置并更改所需的设置。我已经这样做了:

void Application_Start(object sender, EventArgs e)
{
    var newCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    newCulture.NumberFormat.CurrencyDecimalSeparator = ".";
    newCulture.NumberFormat.CurrencyGroupSeparator = ",";
}

但是,如何为应用程序从此时开始处理的所有请求激活这种新文化?还有其他方法可以实现我想做的事情吗?

【问题讨论】:

    标签: .net asp.net internationalization formatting cultureinfo


    【解决方案1】:

    您可以使用Application_BeginRequest 事件为每个请求设置文化。内部事件:

    var newCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    newCulture.NumberFormat.CurrencyDecimalSeparator = ".";
    newCulture.NumberFormat.CurrencyGroupSeparator = ",";
    
    System.Threading.Thread.CurrentThread.CurrentCulture = newCulture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = newCulture;
    

    【讨论】:

    • 这似乎没有任何效果。
    • 刚刚在我的电脑上观察到,文化 en-ZA 的 NumberFormat.CurrencyDecimalSeparator 和 NumberFormat.CurrencyGroupSeparator 的默认格式是“。”和 "," 分别表示它们已经设置为您正在寻找的值。这就是为什么您没有看到任何差异
    • 您使用的是什么 .NET 版本?我相信它们在 3.5 和 4 之间变化,从你所拥有的到我所拥有的,即组的“空格”和小数的逗号。
    • 如果升级到 4,请注意此类更改。
    【解决方案2】:

    在提出了许多问题并进行了许多实验后,我决定可以肯定地说,这样做的唯一方法是使用从开箱即用控件派生的控件,并使用自定义的区域性对象进行自己的格式化。从例如获得您的控制权BoundField 并提供您自己的FormatProvider

    public class BoundReportField : BoundField
    {
        protected virtual string GetDefaultFormatString(FieldFormatTypes formatType)
        {
            var prop = typeof(FormatStrings).GetProperty(formatType.ToString()).GetValue(null, null);
            return prop.ToString();
        }
    
        protected virtual IFormatProvider GetFormatProvider(FieldFormatTypes formatType)
        {
            var info = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            info.NumberFormat.CurrencyDecimalDigits = 0;
            info.NumberFormat.CurrencySymbol = "R";
            info.NumberFormat.CurrencyGroupSeparator = ",";
            info.NumberFormat.CurrencyDecimalSeparator = ".";
            return info;
        }
    
        private FieldFormatTypes _formatType;
        public virtual FieldFormatTypes FormatType
        {
            get { return _formatType; }
            set
            {
                _formatType = value;
                DataFormatString = GetDefaultFormatString(value);
            }
        }
    
        protected override string FormatDataValue(object dataValue, bool encode)
        {
            // TODO Consider the encode flag.
            var formatString = DataFormatString;
            var formatProvider = GetFormatProvider(_formatType);
            if (string.IsNullOrWhiteSpace(formatString))
            {
                formatString = GetDefaultFormatString(_formatType);
            }
            return string.Format(formatProvider, formatString, dataValue);
        }
    }
    

    稍后我将发表一篇包含所有血腥细节的文章。

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2020-01-24
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多