简体   繁体   English

在不同文化中格式化数字

[英]Formatting numbers in different cultures

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));

Output: Output:

2,295

Desired output:所需的 output:

2.295

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}".例如,对于丹麦,他们将价格格式定义为“{0:0},-”,而对于爱尔兰,可能是“€{0:#,##0}”。

When you have different format strings, this does not mean that you have to use InvariantCulture.当您有不同的格式字符串时,这并不意味着您必须使用 InvariantCulture。 If you have a format string for germany eg you format this string using the Culture("de-de"):如果你有一个德国的格式字符串,例如你使用 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,-

Alternatively you can specify your custom number format info :或者,您可以指定您的自定义数字格式信息

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

The normal approach would be to not use an Invariant culture.正常的方法是使用 Invariant 文化。

You do specify the formatting in Invariant style, but the proper symbols would be substituted, #,##0.00 will come out as 1.234,50 or as 1,235.50 depending on the actual culture used.您确实以 Invariant 样式指定格式,但会替换正确的符号, #,##0.00将显示为1.234,501,235.50 ,具体取决于使用的实际文化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM