C# Number Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
In C#, you can display numbers with commas as thousands separators and you can also use the CultureInfo
class to display numbers with thousands separators based on the culture. Here are some examples:
int number = 123456789; string formattedNumber = string.Format("{0:n0}", number);
In this example, the number
variable contains an integer value. The string.Format
method is called with the format string "{0:n0}"
as the first argument, and the number
variable as the second argument. The n0
format specifier specifies that a comma should be added after every three digits of the number. The resulting string is stored in the formattedNumber
variable.
int number = 123456789; CultureInfo culture = new CultureInfo("en-US"); string formattedNumber = number.ToString("N0", culture);
In this example, the number
variable contains an integer value. The CultureInfo
class is used to specify the culture of the formatting. The ToString
method is called on the number
variable with the format string "N0"
as the first argument, and the culture
variable as the second argument. The N0
format specifier specifies that a comma should be added after every three digits of the number. The resulting string is stored in the formattedNumber
variable.
decimal number = 123456.789m; string formattedNumber = number.ToString("0,0.00");
In this example, the number
variable contains a decimal value. The ToString
method is called on the number
variable with the format string "0,0.00"
as the argument. The 0
format specifier specifies that a digit should be displayed in that position, the ,
character specifies the thousands separator, and the .
character specifies the decimal separator. The resulting string is stored in the formattedNumber
variable.
C# display number with thousands separator
int number = 1234567; Console.WriteLine("{0:N0}", number); // Outputs: 1,234,567
Displaying float with thousands separator in C#
float floatValue = 1234567.89f; Console.WriteLine($"{floatValue:N2}"); // Outputs: 1,234,567.89
C# display double with thousands separator
double doubleValue = 1234567.89; Console.WriteLine($"{doubleValue:N2}"); // Outputs: 1,234,567.89
Formatting currency with thousands separator in C#
decimal currencyValue = 1234567.89m; Console.WriteLine($"{currencyValue:C}"); // Outputs: $1,234,567.89
Displaying decimal with thousands separator in C#
decimal decimalValue = 1234567.89m; Console.WriteLine($"{decimalValue:N2}"); // Outputs: 1,234,567.89
C# format long with thousands separator
long longValue = 123456789012345; Console.WriteLine("{0:N0}", longValue); // Outputs: 123,456,789,012,345
C# format number with thousands separator and decimal places
double number = 1234567.89; Console.WriteLine($"{number:N2}"); // Outputs: 1,234,567.89
Displaying percentages with thousands separator in C#
double percentageValue = 0.753; Console.WriteLine($"{percentageValue:P2}"); // Outputs: 75.30%
Rounding and formatting numbers with thousands separator in C#
double number = 1234567.89; string roundedAndFormatted = Math.Round(number, 2).ToString("N2");