在不同的文化格式化数字不同、数字、文化

2023-09-03 15:07:07 作者:你困住我,年深月久

假设一个不变的文化,是可以定义的格式不同的组分隔符 - ?比逗号

Assuming an invariant culture, is it possible to define a different group separator in the format - than the comma?

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine(String.Format("{0:#,##0}", 2295));

输出:

2,295

所需的输出:

Desired output:

2.295

在不变的文化是一个要求,因为从很多不同的语言环境中的货币被格式化的格式化字符串,对已用户自定义。如丹麦,他们所定义的价格格式为{0:0} - ,而爱尔兰也可能是€{0:#,## 0}。

The invariant culture is a requirement because currencies from many different locales are being formatted with format strings, that have been user defined. Ie for Denmark they have defined the price format to be "{0:0},-", while for Ireland it might be "€{0:#,##0}".

推荐答案

当你有不同的格式字符串,这并不意味着你必须使用InvariantCulture的。如果你有一个格式字符串德国例如您使用格式化的文化(反德),此字符串:

When you have different format strings, this does not mean that you have to use InvariantCulture. If you have a format string for germany e.g. you format this string using the Culture("de-de"):

String.Format(CultureInfo.GetCultureInfo( "de-de" ), "{0:0},-", 2295) //will result in 2.295,-
String.Format(CultureInfo.GetCultureInfo( "en-us" ), "{0:0},-", 2295) //will result in 2,295,-

另外,您可以指定您的自定义number格式信息:

NumberFormatInfo nfi = new NumberFormatInfo( )
{
    CurrencyGroupSeparator = ":"
};
String.Format(nfi, "{0:0},-", 2295) //will result in 2:295,-