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:
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.
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.
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:
- How to reverse string in C# using while loop?
- Check if an Array is Empty in C#
- How to convert string to double with 2 decimals in C#.Net?
- Convert Object to JSON Without Escape Characters in C#.NET
- Format Number with Commas and Decimal in C#
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…