Formatting numbers in different cultures
Asked Answered
M

2

5

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

Magnesia answered 24/5, 2011 at 12:1 Comment(0)
U
9

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,-
Uncommitted answered 24/5, 2011 at 12:11 Comment(2)
I do not understand your answer. Could you elaborate?Magnesia
I was not clear enough. I did not have the information about what culture it was (at least not directly in an "en-us"-kinda format. So I was hoping to let the customer write their priceformatting-strings in a universal format without regards for what culture it would be executed under. I guess that is not possible, so I added a column with the culture-identified and use it as you describe above. :)Magnesia
B
3

The normal approach would be to not use an Invariant culture.

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.

Bruno answered 24/5, 2011 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.