【问题标题】:How can I Change the decimal saperator to "," (comma) without changing the culture settings [closed]如何在不更改文化设置的情况下将小数点分隔符更改为“,”(逗号)[关闭]
【发布时间】:2021-06-01 15:43:06
【问题描述】:

我正在使用字符串格式 {###,###,##0.00} 和货币格式 {0:N2} 这给了我 9,203.25 ,但我需要 9.203,25 不改变文化。请提出格式更改建议。 谢谢..

【问题讨论】:

  • String.Format 专门用于将数据转换为人类可读的格式,包括本地化。它不是数据序列化格式。如果您不想本地化,则不应使用String.Format;为什么你认为你需要这样做?

标签: c# asp.net .net model-view-controller


【解决方案1】:

您可以从当前文化中复制NumberFormatInfo,然后根据自己的喜好进行编辑。

属性NumberDecimalSeparatorNumberGroupSeparator 控制小数点和千位分隔符。

var info = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
info.NumberDecimalSeparator = ",";
info.NumberGroupSeparator = ".";
        
Console.WriteLine(string.Format(info, "{0:N2}", 9203.25)); // 9.203,25

See it here.

或者,如果您不想从当前文化继承任何格式设置,您可以从不变文化开始:

var info = new NumberFormatInfo()
{
    NumberDecimalSeparator = ",",
    NumberGroupSeparator = ".",
};

Console.WriteLine(string.Format(info, "{0:N2}", 9203.25)); // 9.203,25

See it here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多