Use a custom thousand separator in C#

I suggest you find a NumberFormatInfo which most closely matches what you want (i.e. it’s right apart from the thousands separator), call Clone() on it and then set the NumberGroupSeparator property. (If you’re going to format the numbers using currency formats, you need to change CurrencyGroupSeparator instead/as well.) Use that as the format info for your calls to string.Format etc, and you should be fine. For example:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        NumberFormatInfo nfi = (NumberFormatInfo)
            CultureInfo.InvariantCulture.NumberFormat.Clone();
        nfi.NumberGroupSeparator = " ";

        Console.WriteLine(12345.ToString("n", nfi)); // 12 345.00
    }
}

Leave a Comment