Introduction: Why Commas in Numbers Are Not Just About Aesthetics
\n\nImagine: you display an employee's salary on screen — 1500000 rubles. Your eyes glaze over, zeros blur together, and you count the digits three times. Now imagine 1,500,000 — instant comprehension of the amount. The difference is enormous.
On Stack Overflow, the question ".NET String.Format() to add commas in thousands place for a number" has garnered tens of thousands of views. This means developers worldwide are tackling the same task: how to format a number with thousands separators in C# beautifully and correctly. Today, we'll cover all approaches — from the classic String.Format to modern C# features.
If you code in C# and work with reports, financial data, or simply want your UI to look professional — this article is for you. Let's go!
\n\nWhat is String.Format() and Why Do You Need It?
\n\nString.Format() is a static method that allows you to insert variable values into a template string with formatting control. Unlike simple concatenation, it offers flexibility: you can specify exactly how to display a number — with separators, with a fixed number of decimal places, in currency format, and so on.
The syntax is simple: String.Format("{0:N}", number), where 0 is the argument index and N is the format specifier. But the devil, as always, is in the details.
The Main Method: String.Format with the "N" Format
\n\nThe simplest and most obvious way to add commas to a number is to use the "N" format specifier (short for Number). It automatically adds thousands separators and sets the number of decimal places (2 by default).
int number = 1234567;\nstring formatted = String.Format("{0:N}", number);\nConsole.WriteLine(formatted); // Output: 1,234,567.00\n\n\nIf you don't need decimal places, specify "N0" — zero means "zero decimal places":
string formatted = String.Format("{0:N0}", number);\nConsole.WriteLine(formatted); // Output: 1,234,567\n\n\nImportant: The thousands separator symbol depends on the current culture (CultureInfo). In the Russian locale, it will be a space (1 234 567), in the English locale — a comma. If you specifically need a comma regardless of regional settings, read on.
Alternatives: ToString("N") and String Interpolation
\n\nModern C# offers more elegant ways. Instead of String.Format, you can call the ToString method directly on the number:
string formatted = number.ToString("N0");\nConsole.WriteLine(formatted); // 1,234,567\n\n\nAnd if you're using C# 6 or higher, string interpolation makes the code even more readable:
\n\nstring formatted = $"{number:N0}";\nConsole.WriteLine(formatted); // 1,234,567\n\n\nInterpolation is essentially the same as String.Format, but syntactically more pleasant. It compiles into a String.Format call under the hood, so performance is identical.
Fine-Tuning: Custom Format Specifiers
\n\nSometimes the built-in "N" format is not enough. For example, you might need to:
- \n
- Use a space instead of a comma \n
- Show a "+" sign for positive numbers \n
- Align numbers by width \n
For such cases, custom format strings exist. Here's how to manually set a thousands separator using the # or 0 symbol:
int number = 1234567;\nstring customFormat = "#,##0";\nstring formatted = number.ToString(customFormat);\nConsole.WriteLine(formatted); // 1,234,567\n\n\nLet's break down what "#,##0" means:
- \n
#— optional digit placeholder