Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 229
Kjensen Avatar asked May 24 '11 12:05

Kjensen


People also ask

How are numbers formatted in Europe?

Now let's get started. In most European countries, a comma is used to separate the integral part of a number from the decimal part. This means, for example, that three hundred euros and ten cents is written as 300,10—with a comma as a decimal marker.

Which countries use comma instead of decimal?

The three most spoken international auxiliary languages, Ido, Esperanto, and Interlingua, all use the comma as the decimal separator. Interlingua has used the comma as its decimal separator since the publication of the Interlingua Grammar in 1951.

Which countries use comma as thousand separator?

The decimal separator is also called the radix character. Likewise, while the U.K. and U.S. use a comma to separate groups of thousands, many other countries use a period instead, and some countries separate thousands groups with a thin space.


2 Answers

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,-
like image 122
PVitt Avatar answered Oct 01 '22 11:10

PVitt


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.

like image 42
Henk Holterman Avatar answered Oct 01 '22 12:10

Henk Holterman