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
How to convert an integer to string with padding zero in C#?
Converting an integer to a string with padding zeros is a common requirement in C# programming. This allows you to format numbers with a consistent width by adding leading zeros. There are several approaches to achieve this, each with its own syntax and use cases.
Syntax
Following are the common syntaxes for zero-padding integers −
// Using PadLeft method
number.ToString().PadLeft(width, '0');
// Using custom numeric format
number.ToString("0000");
// Using standard numeric format
number.ToString("D4");
// Using string interpolation
$"{number:0000}"
Using PadLeft Method
The PadLeft method pads the beginning of a string with a specified character to achieve a target width. This approach first converts the integer to a string, then adds padding −
using System;
class Program {
public static void Main() {
int number = 5;
Console.WriteLine("Number: {0}", number);
var numberString = number.ToString().PadLeft(4, '0');
Console.WriteLine("Padded String: {0}", numberString);
int largerNumber = 123;
var largerPadded = largerNumber.ToString().PadLeft(6, '0');
Console.WriteLine("Larger Number Padded: {0}", largerPadded);
}
}
The output of the above code is −
Number: 5 Padded String: 0005 Larger Number Padded: 000123
Using Custom Numeric Format
You can specify a custom format pattern using zeros to define the minimum number of digits. Each zero represents a digit position that will be filled with leading zeros if needed −
using System;
class Program {
public static void Main() {
int number = 5;
Console.WriteLine("Number: {0}", number);
var numberString = number.ToString("0000");
Console.WriteLine("Padded String: {0}", numberString);
int negativeNumber = -42;
var negativePadded = negativeNumber.ToString("0000");
Console.WriteLine("Negative Padded: {0}", negativePadded);
}
}
The output of the above code is −
Number: 5 Padded String: 0005 Negative Padded: -0042
Using Standard Numeric Format (D)
The D format specifier creates a decimal string with a specified minimum number of digits. This approach is concise and specifically designed for integer formatting −
using System;
class Program {
public static void Main() {
int number = 5;
Console.WriteLine("Number: {0}", number);
var numberString = number.ToString("D4");
Console.WriteLine("Padded String: {0}", numberString);
int zeroValue = 0;
var zeroPadded = zeroValue.ToString("D6");
Console.WriteLine("Zero Padded: {0}", zeroPadded);
}
}
The output of the above code is −
Number: 5 Padded String: 0005 Zero Padded: 000000
Using String Interpolation
String interpolation with format specifiers provides a modern, readable way to format numbers inline within string literals −
using System;
class Program {
public static void Main() {
int number = 5;
Console.WriteLine("Number: {0}", number);
var numberString = $"{number:0000}";
Console.WriteLine("Padded String: {0}", numberString);
int[] numbers = {1, 25, 456};
foreach(int num in numbers) {
Console.WriteLine($"Formatted: {num:D5}");
}
}
}
The output of the above code is −
Number: 5 Padded String: 0005 Formatted: 00001 Formatted: 00025 Formatted: 00456
Comparison of Methods
| Method | Performance | Readability | Use Case |
|---|---|---|---|
| PadLeft | Slower | Clear intent | When padding with different characters |
| Custom Format ("0000") | Fast | Very readable | Fixed-width formatting |
| Standard Format ("D4") | Fast | Concise | Decimal integers only |
| String Interpolation | Fast | Modern syntax | Inline formatting in strings |
Conclusion
Converting integers to zero-padded strings in C# can be accomplished through multiple methods. The ToString("D4") and custom format approaches are generally preferred for performance and readability, while PadLeft offers flexibility for different padding characters. String interpolation provides the most modern and readable syntax for inline formatting.
