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
C# Numeric ("N") Format Specifier
The numeric (N) format specifier converts a number to a string with thousands separators and decimal places. It follows the pattern -d,ddd,ddd.ddd? where the minus sign appears for negative numbers, digits are grouped with commas, and decimal places are included.
Syntax
Following is the syntax for using the numeric format specifier −
number.ToString("N") // Default 2 decimal places
number.ToString("N0") // No decimal places
number.ToString("Nn") // n decimal places
string.Format("{0:N}", number) // Using string.Format
Components of Numeric Format
-
-− Negative number symbol (appears only if the number is negative) -
d− A digit (0-9) -
,− Thousands separator (groups digits in sets of three) -
.− Decimal point symbol
Using Default Numeric Format
The default N format displays 2 decimal places −
using System;
using System.Globalization;
class Program {
static void Main() {
double val1 = -5566.789;
int val2 = 1234567;
decimal val3 = 999.5m;
Console.WriteLine(val1.ToString("N", CultureInfo.InvariantCulture));
Console.WriteLine(val2.ToString("N", CultureInfo.InvariantCulture));
Console.WriteLine(val3.ToString("N", CultureInfo.InvariantCulture));
}
}
The output of the above code is −
-5,566.79 1,234,567.00 999.50
Using Precision Specifiers
You can control the number of decimal places using precision specifiers −
using System;
using System.Globalization;
class Program {
static void Main() {
double value = 87987.766543;
Console.WriteLine("N0: " + value.ToString("N0", CultureInfo.InvariantCulture));
Console.WriteLine("N1: " + value.ToString("N1", CultureInfo.InvariantCulture));
Console.WriteLine("N3: " + value.ToString("N3", CultureInfo.InvariantCulture));
Console.WriteLine("N5: " + value.ToString("N5", CultureInfo.InvariantCulture));
}
}
The output of the above code is −
N0: 87,988 N1: 87,987.8 N3: 87,987.766 N5: 87,987.76654
Comparison with Other Format Specifiers
| Format | Description | Example (1234.56) |
|---|---|---|
| N | Numeric with thousands separators | 1,234.56 |
| F | Fixed-point (no thousands separators) | 1234.56 |
| C | Currency format | $1,234.56 |
Using with Different Data Types
using System;
using System.Globalization;
class Program {
static void Main() {
int intValue = 12345;
float floatValue = 12345.678f;
double doubleValue = 12345.6789;
decimal decimalValue = 12345.6789m;
Console.WriteLine("int: " + intValue.ToString("N2", CultureInfo.InvariantCulture));
Console.WriteLine("float: " + floatValue.ToString("N2", CultureInfo.InvariantCulture));
Console.WriteLine("double: " + doubleValue.ToString("N2", CultureInfo.InvariantCulture));
Console.WriteLine("decimal: " + decimalValue.ToString("N2", CultureInfo.InvariantCulture));
}
}
The output of the above code is −
int: 12,345.00 float: 12,345.68 double: 12,345.68 decimal: 12,345.68
Conclusion
The numeric (N) format specifier is ideal for displaying numbers in a readable format with thousands separators and controlled decimal places. It automatically handles negative signs and provides consistent formatting across different numeric data types, making it perfect for financial and statistical displays.
