How to Format Number Thousand Separator with Space in C#?

Do you want to display large numbers in a more readable format in C#? In this C# tutorial, I will explain how to format number thousand separator with space in C#.

To format a number with a thousand separator as a space in C#, use the ToString method with a custom format string. You can apply ToString(“N0”, new CultureInfo(“fr-FR”)) on your numeric variable, where the CultureInfo is set to a culture that uses space as a thousand separator, like French (“fr-FR”). This will format your number accordingly.

Format Number Thousand Separator with Space in C#

Before we get into the solutions, let’s understand why we need to format numbers. Take the number 1000000, for example. Reading this number at a glance is a bit challenging. Now, consider “1 000 000”, which is way easier to read. The spaces separating the thousands make it clear that we’re dealing with one million.

To format number thousand separator with space in C#, you can use one of the three methods below.

  • Using String.Format Method
  • Using ToString Method
  • Using CultureInfo

Using String.Format Method

The String.Format method is one of the simplest ways to format numbers in C#. You can use custom format specifiers to insert spaces as thousand separators.

Here’s a complete example:

using System;

class Program
{
    static void Main()
    {
        int number = 1000000;
        string formattedNumber = String.Format("{0:n}", number).Replace(",", " ");
        Console.WriteLine(formattedNumber);
    }
}

When you run this code, it will output:

1 000 000.00

You can check the screenshot below after I run the code using Visual Studio.

c# format number thousand separator space

Using ToString Method

Another method you can use is the ToString method with custom formatting.

Here’s how you can format number thousand separator with space in C#.

using System;

class Program
{
    static void Main()
    {
        int number = 1000000;
        string formattedNumber = number.ToString("#,##0").Replace(",", " ");
        Console.WriteLine(formattedNumber);
    }
}

Once you run the code using a console application using Visual Studio, you can see the output like below:

Format Number Thousand Separator with Space in C#

Using CultureInfo

If you want more control over the formatting, you can use CultureInfo.

Here’s a complete example:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        int number = 1000000;
        CultureInfo ciClone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
        ciClone.NumberFormat.NumberGroupSeparator = " ";
        string formattedNumber = number.ToString("N0", ciClone);
        Console.WriteLine(formattedNumber);
    }
}

Once you execute the C# code, you can see the output in the screenshot below:

How to format number thousand separator with space in C#

Conclusion

In this blog post, we’ve looked at several ways to format number thousand separator with space in C#. We’ve explored using String.Format, ToString, and CultureInfo for this purpose. Formatting numbers with spaces makes them easier to read and can significantly improve the user experience of your application.

You may also like the following tutorials: