In this C# tutorial, I will explain how to format numbers without commas in C# using different methods and examples.
To format numbers without commas in C#, use the ToString method with the “N0” format specifier, combined with a CultureInfo that does not use commas for number grouping. For example, number.ToString(“N0”, CultureInfo.InvariantCulture) will format the number without commas. This effectively displays your number in a plain numeric format.
Format Numbers Without Commas in C#
Commas in number formatting are a standard way to make large numbers more readable. However, in certain situations, the comma could interfere with the way the number is interpreted.
To format numbers without commas in C#, you can use one of the below 3 methods.
- Using ToString() Method
- Using String.Format() Method
- Using String Interpolation
Using ToString() Method
The simplest way to remove commas from numbers in C# is to use the ToString()
method. This method comes with a variety of formatting options. Here’s an example:
using System;
namespace RemoveCommasExample
{
class Program
{
static void Main(string[] args)
{
int number = 1000000;
string formattedNumber = number.ToString("D");
Console.WriteLine(formattedNumber);
}
}
}
Here, the format string “D” is used to format the number as a decimal without commas in C#.
Once you run the above code using Visual Studio, you can see the output below:
Using String.Format() Method
Another way to format numbers without commas in C# is to use the String.Format()
method. This method allows you to format strings in various ways, including removing commas from numbers.
using System;
namespace RemoveCommasExample
{
class Program
{
static void Main(string[] args)
{
int number = 1000000;
string formattedNumber = String.Format("{0:D}", number);
Console.WriteLine(formattedNumber);
}
}
}
Again, the “D” specifies that the number should be formatted as a decimal without commas.
Using String Interpolation
C# 6.0 and later versions support string interpolation, which is another efficient way to format strings, including numbers. Here’s an example:
using System;
namespace RemoveCommasExample
{
class Program
{
static void Main(string[] args)
{
int number = 1000000;
string formattedNumber = $"{number:D}";
Console.WriteLine(formattedNumber);
}
}
}
With string interpolation, you can inline the format specifier directly within the string itself, making it more readable and easier to understand.
Cultural Considerations
Always be cautious of the cultural settings when formatting numbers. In some cultures, a comma might be used as a decimal separator. Always specify the culture if your application is meant to be culture-agnostic.
using System;
using System.Globalization;
namespace RemoveCommasExample
{
class Program
{
static void Main(string[] args)
{
int number = 1000000;
CultureInfo culture = new CultureInfo("en-US");
string formattedNumber = number.ToString("D", culture);
Console.WriteLine(formattedNumber);
}
}
}
Once you run the code, you can see the output below in the screenshot.
Conclusion
Formatting numbers without commas in C# is a very common requirement. In this tutorial, we check how to use the ToString()
method, the String.Format()
method, or string interpolation to format a number without comma in C#.
You may also like:
- Convert int to float with 2 Decimal Places in C#
- How to Format Number with Commas and Decimal in C#?
- Format Numbers Without Thousand Separators in C#
- How to Format Number Thousand Separator with Space 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…