Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:

2,295

Desired 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}".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
278 views
Welcome To Ask or Share your Answers For Others

1 Answer

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,-

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,-

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...