我如何可以覆盖货币格式当前区域性的ASP.NET Web应用程序? [英] How can I override the currency formatting for the current culture for an ASP.NET web application?

查看:181
本文介绍了我如何可以覆盖货币格式当前区域性的ASP.NET Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

货币小数和千位分隔符为EN-ZA地区是''和''分别,但常用的分隔符是'。对于小数,再加上我的用户想','为千位分隔符。我要设置这些全球,让我只需要使用 {0:C} 格式字符串为我所有的货币领域,而无需任何明确的格式的ToString 通话。

The currency decimal and thousand separators for the en-ZA region are ',' and ' ' respectively, but the separators in common use are '.' for decimal, plus my user wants ',' for the thousands separator. I wish to set these globally, so that I only have to use the {0:C} format string for all my currency fields, without having to any explicit Format or ToString calls.

在R100k的估计报告时,我将preFER才能够做到这一点,而不在Web服务器上改变企业文化设置,因为我还需要设置小数位货币到零,因为分不想要最多等等。我不想随意设置整个文化为零的地方,只有我一个。这个应用程序。

I would prefer to be able to do this without changing the culture settings on the web server, as I also need to set the decimal places for currency to zero, as cents are not wanted when reporting on estimates of R100k and up etc. I wouldn't want to arbitrarily set the whole culture to zero places, just one for this application.

在评论他的回答在这个问题,乔恩斯基特表明克隆当前区域性和设置和更改所需的设置。我已经做到了这一点,如下所示:

In comments to his answer on this question, Jon Skeet suggests cloning the current culture and setting and changing the required settings. I have done this as follows:

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

然而,如何激活我的所有请求新文化的应用程序,从这个角度上处理?有另一种方式来实现我想要做的?

However, how to I activate that new culture for all requests the application handles from this point on? Is there another way to achieve what I wish to do?

推荐答案

已经问了很多问题,并做了很多的实验,我已经决定了它的安全声明,这样做的唯一方法是使用来自出来的派生的控件框控件,并使用自定义的文化对象做你自己的格式。例如从派生的控制的BoundField ,并提供自己的 FormatProvider

Having asked many questions and done many experiments, I have decided that it's safe to state that the only way of doing this is to use controls derived from the out of box controls and do your own formatting using a customised culture object. Derive your control from e.g. BoundField and provide your own 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);
    }
}

我会在稍后发表的一篇文章的所有血淋淋的细节。

I will publish an article later with all the gory details.

这篇关于我如何可以覆盖货币格式当前区域性的ASP.NET Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆