Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print number with commas as 1000 separators in C#
In C#, you can format numbers with commas as thousand separators using various approaches. The most common and efficient methods involve using format strings with the ToString() method or composite formatting.
Syntax
Following is the syntax for formatting numbers with thousand separators −
number.ToString("N") // Standard numeric format
number.ToString("#,##0.##") // Custom format with commas
string.Format("{0:N}", number) // Composite formatting
Using Standard Numeric Format ("N")
The "N" format specifier automatically adds thousand separators and formats the number according to the current culture −
using System;
public class Program {
public static void Main() {
double number1 = 1234567.89;
int number2 = 1000000;
decimal number3 = 9876543.21m;
Console.WriteLine("Using 'N' format:");
Console.WriteLine(number1.ToString("N"));
Console.WriteLine(number2.ToString("N"));
Console.WriteLine(number3.ToString("N"));
}
}
The output of the above code is −
Using 'N' format: 1,234,567.89 1,000,000.00 9,876,543.21
Using Custom Format Strings
Custom format strings give you more control over the number of decimal places and formatting −
using System;
public class Program {
public static void Main() {
double number = 1234567.89;
Console.WriteLine("Custom format examples:");
Console.WriteLine(number.ToString("#,##0")); // No decimals
Console.WriteLine(number.ToString("#,##0.0")); // 1 decimal
Console.WriteLine(number.ToString("#,##0.00")); // 2 decimals
Console.WriteLine(number.ToString("#,##0.####")); // Up to 4 decimals
// Handling integers
int intNumber = 1000000;
Console.WriteLine(intNumber.ToString("#,##0"));
}
}
The output of the above code is −
Custom format examples: 1,234,568 1,234,567.9 1,234,567.89 1,234,567.89 1,000,000
Using String Interpolation and Composite Formatting
You can also use string interpolation or string.Format() for inline formatting −
using System;
public class Program {
public static void Main() {
double price = 1234567.50;
int quantity = 50000;
// String interpolation
Console.WriteLine($"Price: {price:N2}");
Console.WriteLine($"Quantity: {quantity:#,##0}");
// Composite formatting
Console.WriteLine(string.Format("Total: {0:C}", price * quantity));
Console.WriteLine("Formatted: {0:#,##0.00}", price);
}
}
The output of the above code is −
Price: 1,234,567.50 Quantity: 50,000 Total: $61,728,375,000.00 Formatted: 1,234,567.50
Comparison of Formatting Methods
| Method | Use Case | Example |
|---|---|---|
| ToString("N") | General numeric formatting with culture-specific separators | 1,234.56 |
| ToString("#,##0.##") | Custom control over decimal places | 1,234.56 |
| ToString("C") | Currency formatting with thousand separators | $1,234.56 |
| String interpolation {value:N} | Inline formatting within strings | Value: 1,234.56 |
Conclusion
C# provides multiple ways to format numbers with thousand separators. The ToString("N") method is the simplest approach, while custom format strings like "#,##0.##" offer precise control over decimal places and formatting appearance.
