You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用nl-NL/nl-BE文化格式时Blazor货币显示异常问题

问题原因与解决方案

你遇到的问题是因为.NET框架中默认的nl-NL/nl-BE文化的货币格式遵循欧盟统一规范,千位分隔符为逗号、小数分隔符为点,和你需要的荷兰本地传统格式(点作千位分隔符、逗号作小数分隔符)不一致。

解决方案一:修改文化的数字格式信息

通过克隆nl-NL文化的NumberFormatInfo并调整分隔符,实现自定义货币格式,同时不影响全局文化设置:

PrijsDisplay.razor 修改后代码

@using System.Globalization

<span>@FormattedPrijs</span>

@code {
    [Parameter]
    public required decimal Prijs { get; set; }

    private string FormattedPrijs => FormatDutchLocalCurrency(Prijs);

    private string FormatDutchLocalCurrency(decimal amount)
    {
        var dutchCulture = new CultureInfo("nl-NL");
        // 克隆原文化的格式信息,避免修改全局设置
        var customNumberFormat = (NumberFormatInfo)dutchCulture.NumberFormat.Clone();
        
        // 设置千位分隔符为点
        customNumberFormat.CurrencyGroupSeparator = ".";
        // 设置小数分隔符为逗号
        customNumberFormat.CurrencyDecimalSeparator = ",";
        // 设置货币符号与金额无空格(如需空格可改为2,即€ 101.099,00)
        customNumberFormat.CurrencyPositivePattern = 0;

        return amount.ToString("C", customNumberFormat);
    }
}

解决方案二:直接使用自定义格式字符串

如果不需要依赖文化设置,也可以直接写死格式字符串,更简洁:

PrijsDisplay.razor 修改后代码

<span>@Prijs.ToString("€#.##0,00")</span>

注:如果需要处理负数格式,可调整为€#.##0,00;€-#.##0,00,对应负数显示为€-101.099,00

为什么默认nl-NL不符合预期

.NET中的nl-NL文化货币格式采用的是欧盟统一的金融格式规范,而荷兰本地传统格式(点千分位、逗号小数位)属于更本地化的显示方式,并未作为该文化的默认格式,因此需要手动调整。

内容的提问来源于stack exchange,提问作者SmugSnail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:30:07