C#
.NET
String.Format
number formatting
thousands separator

.NET String.Format to add commas in thousands place for a number

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In .NET, adding commas to the thousands place is usually just number formatting, not manual string manipulation. The most common answer is to use String.Format with a numeric format string such as N0, or the equivalent interpolated or ToString form. The exact output still depends on culture, so the visible separator may be a comma, a period, or another grouping symbol depending on locale.

The Basic String.Format Answer

For an integer with grouping separators and no decimal places:

csharp
1using System;
2
3int number = 1234567890;
4string formatted = String.Format("{0:N0}", number);
5Console.WriteLine(formatted);

In an English locale, this prints:

text
1,234,567,890

N0 means number format with zero decimal places.

Keep Decimals When You Need Them

If the value is decimal and you want two fractional digits, use N2.

csharp
1using System;
2
3decimal value = 1234567.89m;
4string formatted = String.Format("{0:N2}", value);
5Console.WriteLine(formatted);

That produces a grouped number with two decimal places in the active culture.

Culture Affects the Separator

The title mentions commas, but .NET formatting is culture-aware. In some locales, the grouping separator is a period or a space.

csharp
1using System;
2using System.Globalization;
3
4int number = 1234567;
5Console.WriteLine(String.Format(CultureInfo.GetCultureInfo("en-US"), "{0:N0}", number));
6Console.WriteLine(String.Format(CultureInfo.GetCultureInfo("de-DE"), "{0:N0}", number));

So if you require literal commas specifically, you need the appropriate culture or a custom format strategy.

Custom Number Formats Also Work

You can use a custom numeric pattern when you want more explicit control.

csharp
1using System;
2
3int number = 1234567;
4string formatted = String.Format("{0:#,##0}", number);
5Console.WriteLine(formatted);

This gives grouped output too. In many day-to-day cases, though, N0 is easier to read and maintain.

ToString and Interpolation Are Equivalent Ideas

String.Format is not the only way to do this. ToString often reads more cleanly.

csharp
1using System;
2
3int number = 1234567;
4Console.WriteLine(number.ToString("N0"));
5Console.WriteLine($"{number:N0}");

These are all using the same underlying .NET formatting system.

When You Need a Specific Culture

If the application should always display U.S.-style commas regardless of the machine locale, pass an explicit culture instead of relying on the current thread culture.

csharp
1using System;
2using System.Globalization;
3
4int number = 1234567;
5string formatted = String.Format(CultureInfo.GetCultureInfo("en-US"), "{0:N0}", number);
6Console.WriteLine(formatted);

This matters in exported reports, APIs that produce human-readable text, and server-side systems that may run under a different locale than the users expect.

Pick the Format Based on Output Needs

A useful rule is:

  • use N0 for grouped integers
  • use N2 or another N precision for grouped decimals
  • use custom formats only when the standard format strings are not enough

That keeps most formatting code simple and conventional.

Formatting Should Happen at the Output Boundary

Thousands separators are presentation details, not storage details. Once you turn a number into a formatted string, it is meant for display, logs, or reports. Keep values numeric for calculations and format them only when you are actually presenting them to a person.

Common Pitfalls

  • Assuming the thousands separator will always be a comma regardless of culture.
  • Manually inserting commas into strings instead of using the built-in numeric format system.
  • Using N0 when decimal places should actually be preserved.
  • Reaching for a custom format string when a standard numeric format already does the job.
  • Forgetting that string interpolation and ToString support the same numeric formatting rules as String.Format.

Summary

  • The usual .NET answer is String.Format("{0:N0}", number).
  • 'N0 adds grouping separators and shows no decimal places.'
  • 'N2 keeps two decimal places while still grouping thousands.'
  • The visible separator depends on culture.
  • 'ToString and interpolation use the same formatting rules and are often simpler alternatives.'

Course illustration
Course illustration

All Rights Reserved.