4

ASP.NET Core 中的本地化系统发生了很大变化。在CurrentCulture当前线程中不再可用。

我正在尝试按照此处的说明设置NumberFormat属性,以自定义显示货币的格式,但显然这种方式不再可能。那么如何设置属性呢?CurrentCultureNumberFormatCurrentCulture

在 asp.net core 之前用于此的代码:

CurrentCulture modified = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
Thread.CurrentThread.CurrentCulture = modified;
var numberFormat = modified.NumberFormat;
numberFormat.CurrencySymbol = "RM";
numberFormat.CurrencyDecimalDigits = 2;
numberFormat.CurrencyDecimalSeparator = ".";
numberFormat.CurrencyGroupSeparator = ",";

然后例如在我想要做的代码中的某个地方:

string fMoney;
fMoney = money.ToString("C");
return fMoney;

并期望numberFormat应用这些设置。

4

1 回答 1

5

您可以添加UseRequestLocalizationStartup.csConfigure()中的方法:

    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();
        app.UseRequestLocalization(new RequestCulture(new CultureInfo("es")));
        app.Run(async (context) =>
        {
            context.Response.ContentType = "text/html";
            await context.Response.WriteAsync(HtmlEncoder.Default.HtmlEncode(1000.5f.ToString("C")));
        });
    }

结果:

1.000,50 欧元</p>

此外,更重要的是原始问题:

    public void Configure(IApplicationBuilder app)
    {
        app.UseIISPlatformHandler();

        var modified = new CultureInfo(CultureInfo.DefaultThreadCurrentCulture.DisplayName);
        modified.NumberFormat.CurrencySymbol = "RM";
        modified.NumberFormat.CurrencyDecimalDigits = 2;
        modified.NumberFormat.CurrencyDecimalSeparator = ".";
        modified.NumberFormat.CurrencyGroupSeparator = ",";

        app.UseRequestLocalization(new RequestCulture(modified));
        app.Run(async (context) =>
        {
            context.Response.ContentType = "text/html";
            await context.Response.WriteAsync(HtmlEncoder.Default.HtmlEncode(1000.5f.ToString("C")));
        });
    }

结果:

RM1,000.50

于 2016-04-21T19:46:39.703 回答