thousand separator with C#

If you want to output numbers with thousand separators, this can lead to unpleasant effects in multilingual systems. For example, if the data is in “German” format, but the system is set to English, a misinterpretation can occur here, and the double value 110,045 is suddenly displayed or processed as 110045. (the decimal point is then simply interpreted as a separator and makes a few thousand instead of a few hundred).

the formatting

To format a number as a string with a thousand separator, there is a very simple approach:

string.Format(new System.Globalization.CultureInfo("de-DE"), "{0:0,0}", 10000000)
// output: 10.000.000

Here, regardless of the system language, the German thousand separator (i.e. the dot) is used. Of course you can use any CultureInfo you like to get other separators. (Information on the use and creation of CultureInfo can be found here).

formatting in XAML with binding

Of course, the whole can also be used comfortably in XAML binding. In the beginning, I used bridges with string properties for the binding, but now I format my data easily without “bloat code”.

<Label 
  Text="{Binding MyCounterValue, StringFormat='{0:#,0}'}"
  FontSize="22" 
  FontAttributes="Bold" />

Note: If Visual Studio shows a lot of errors here, this is not that bad. The application can still be compiled and started, and as soon as you close the XAML code again, the errors will disappear. Unfortunately I haven’t found out how to implement this without error markers, or what the source of the errors is.

If you want to read more about the formatting possibilities in C#, you can find a lot of information in the official Microsoft documentation.