- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert an integer to string with padding zero in C#?
There are several ways to convert an integer to a string in C#.
PadLeft − Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character
ToString − Returns a string that represents the current object.
String Interpolation − The $ special character identifies a string literal as an interpolated string. This feature is available starting with C# 6.
Example using string padding−
Example
using System; namespace DemoApplication{ 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); Console.ReadLine(); } } }
Output
The output of the above code is
Number: 5 Padded String: 0005
Example using explicit form −
Example
using System; namespace DemoApplication{ 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); Console.ReadLine(); } } }
Output
The output of the above code is
Number: 5 Padded String: 0005
Example using short form format specifier −
Example
using System; namespace DemoApplication{ 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); Console.ReadLine(); } } }
Output
The output of the above code is
Number: 5 Padded String: 0005
Example using string interpolation−
Example
using System; namespace DemoApplication{ class Program{ public static void Main(){ int number = 5; Console.WriteLine("Number: {0}", number); var numberString = $"{number:0000}"; Console.WriteLine("Padded String: {0}", numberString); Console.ReadLine(); } } }
Output
The output of the above code is
Number: 5 Padded String: 0005
- Related Articles
- Convert an integer to a hex string in C++
- How to convert String to Integer and Integer to String in Java?
- C++ Program to convert the string into an integer
- How to convert a string to a integer in C
- How to convert an integer to a hexadecimal string in Python?
- How to convert an integer to a octal string in Python?
- C# Program to Convert Integer to String
- Convert a String to Integer Array in C/C++
- C# program to convert binary string to Integer
- Java Program to convert integer to String with Map
- How to convert a string of any base to an integer in JavaScript?
- How to convert a string vector into an integer vector in R?
- C# Program to convert integer array to string array
- Haskell Program to convert the string into an integer
- How to convert a string into integer in JavaScript?

Advertisements