How to Format Numbers with Thousands Separators in C#: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Commas in Numbers Are Not Just About Aesthetics

\n\n

Imagine: 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.

\n\n

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.

\n\n

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\n

What is String.Format() and Why Do You Need It?

\n\n

String.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.

\n\n

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.

\n\n

The Main Method: String.Format with the "N" Format

\n\n

The 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).

\n\n
int number = 1234567;\nstring formatted = String.Format("{0:N}", number);\nConsole.WriteLine(formatted); // Output: 1,234,567.00\n
\n\n

If you don't need decimal places, specify "N0" — zero means "zero decimal places":

\n\n
string formatted = String.Format("{0:N0}", number);\nConsole.WriteLine(formatted); // Output: 1,234,567\n
\n\n

Important: 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.

\n\n

Alternatives: ToString("N") and String Interpolation

\n\n

Modern C# offers more elegant ways. Instead of String.Format, you can call the ToString method directly on the number:

\n\n
string formatted = number.ToString("N0");\nConsole.WriteLine(formatted); // 1,234,567\n
\n\n

And if you're using C# 6 or higher, string interpolation makes the code even more readable:

\n\n
string formatted = $"{number:N0}";\nConsole.WriteLine(formatted); // 1,234,567\n
\n\n

Interpolation 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.

\n\n

Fine-Tuning: Custom Format Specifiers

\n\n

Sometimes the built-in "N" format is not enough. For example, you might need to:

\n
    \n
  • Use a space instead of a comma
  • \n
  • Show a "+" sign for positive numbers
  • \n
  • Align numbers by width
  • \n
\n\n

For such cases, custom format strings exist. Here's how to manually set a thousands separator using the # or 0 symbol:

\n\n
int number = 1234567;\nstring customFormat = "#,##0";\nstring formatted = number.ToString(customFormat);\nConsole.WriteLine(formatted); // 1,234,567\n
\n\n

Let's break down what "#,##0" means:

\n
    \n
  • # — optional digit placeholder

Blogs

Book Recommendations