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# Decimal ("D") Format Specifier
The "D" (decimal) format specifier in C# is used to format integer types as a string of decimal digits (0-9). It displays the number with optional zero-padding to achieve a specified minimum length.
Syntax
Following is the syntax for the "D" format specifier −
number.ToString("D") // Basic decimal format
number.ToString("Dn") // Decimal with minimum n digits
Where n is the minimum number of digits. If the number has fewer digits, it will be padded with leading zeros.
Parameters
Precision specifier − An optional integer that specifies the minimum number of digits in the result string. If omitted, uses the minimum digits needed.
Supported types − Integer types only (int, long, short, byte, etc.). Does not work with floating-point types.
Using Basic Decimal Format
The basic "D" format displays the number as-is without any padding −
using System;
class Program {
static void Main() {
int number = 467;
Console.WriteLine("Basic D format: " + number.ToString("D"));
long bigNumber = 123456789;
Console.WriteLine("Long number: " + bigNumber.ToString("D"));
short smallNumber = 42;
Console.WriteLine("Short number: " + smallNumber.ToString("D"));
}
}
The output of the above code is −
Basic D format: 467 Long number: 123456789 Short number: 42
Using Decimal Format with Padding
When you specify a precision, the result is padded with leading zeros to meet the minimum length −
using System;
class Program {
static void Main() {
int val = 877;
Console.WriteLine("No padding: " + val.ToString("D"));
Console.WriteLine("4 digits minimum: " + val.ToString("D4"));
Console.WriteLine("8 digits minimum: " + val.ToString("D8"));
int smallVal = 5;
Console.WriteLine("Small number with D6: " + smallVal.ToString("D6"));
}
}
The output of the above code is −
No padding: 877 4 digits minimum: 0877 8 digits minimum: 00000877 Small number with D6: 000005
Working with Negative Numbers
The "D" format specifier also works with negative integers, preserving the negative sign −
using System;
class Program {
static void Main() {
int negativeNumber = -123;
Console.WriteLine("Negative with D: " + negativeNumber.ToString("D"));
Console.WriteLine("Negative with D6: " + negativeNumber.ToString("D6"));
Console.WriteLine("Negative with D8: " + negativeNumber.ToString("D8"));
}
}
The output of the above code is −
Negative with D: -123 Negative with D6: -000123 Negative with D8: -00000123
Conclusion
The "D" format specifier in C# formats integer values as decimal digits with optional zero-padding. It's useful for creating fixed-width number representations, especially for IDs, codes, or when consistent formatting is required. Remember that it only works with integer types and preserves the negative sign when padding negative numbers.
