How to Format Numbers Without Thousand Separators in C#?

In this C# tutorial, I have explained how to format a number without a thousand separator in C#. We will check, in detail, how to format numbers without the thousand separator in C#.

Format Numbers Without Thousand Separators in C#

I have done proper research and tried to find out the below 3 methods to format numbers without a thousand separators in C#.

  • Using ToString() Method
  • Using String Interpolation
  • Using String.Format()

Using ToString() Method

The simplest way to remove the thousand separator is to use the ToString method with a custom format string in C#. Here is the complete code demonstrating how to use the ToString() method to format a number without a thousand separators.

using System;

namespace RemoveThousandSeparator
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 1234567;
            string formattedNumber = number.ToString("D");
            Console.WriteLine("Using ToString() method:");
            Console.WriteLine(formattedNumber);
        }
    }
}

Once you run the above code in Visual Studio, you can see the output below:

c# format number without thousand separator

Using String Interpolation

String interpolation is another way to format strings and remove a thousand separators in C#.

using System;

namespace RemoveThousandSeparator
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 1234567;
            string formattedNumber = $"{number:D}";
            Console.WriteLine("Using String Interpolation:");
            Console.WriteLine(formattedNumber);
        }
    }
}

After I run the code using Visual Studio, you can see the output like screenshot below.

Format Numbers Without Thousand Separators in C#

Using String.Format()

If you want even more control over string formatting, you can use the String.Format() method to format a number without a thousand separator in C#.

using System;

namespace RemoveThousandSeparator
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 1234567;
            string formattedNumber = String.Format("{0:D}", number);
            Console.WriteLine("Using String.Format() method:");
            Console.WriteLine(formattedNumber);
        }
    }
}

After running the below code using a Windows application using Visual Studio, you can see the output in the screenshot below.

How to Format Numbers Without Thousand Separators in C#

Conclusion

When it comes to formatting numbers in C#, you have several options to remove the thousand separator. Whether you prefer to use the ToString() method, string interpolation, or String.Format(), all these methods can get the job done effectively.

After following the above examples, I hope you can format a number without a thousand separator in C#.

You may also like: