Given a Number N, the task is to pad this number with P number of leading zeros in C#.
Examples:
Input: N = 123 , P = 4 Output: 0123 Input: N = -3 , P = 5 Output: -00003
Method 1: Using string Format() method : The Format() method is used to replace one or more format items in the specified string with the string representation of a specified object.
Step 1: Get the Number N and number of leading zeros P.
Step 2: Convert the number to string and to pad the string, use the string.Format() method with the formatted string argument “{0:0000}” for P= 4.
val = string.Format("{0:0000}", N);Step 3: Return the padded string.
Example :
C#
// C# program to pad an integer // number with leading zeros using System; class GFG{ // Function to pad an integer number // with leading zeros static string pad_an_int(int N, int P) { // string used in Format() method string s = "{0:"; for( int i=0 ; i<P ; i++) { s += "0"; } s += "}"; // use of string.Format() method string value = string.Format(s, N); // return output return value; } // driver code public static void Main(string[] args) { int N = 123; // Number to be pad int P = 5; // Number of leading zeros Console.WriteLine("Before Padding: " + N); // Function calling Console.WriteLine("After Padding: " + pad_an_int(N, P)); } } |
Output:
Before Padding: 123 After Padding: 00123
Method 2: Using ToString() method : The ToString() method is used to convert the numeric value of the current instance to its equivalent string representation.
Step 1: Get the Number N and number of leading zeros P.
Step 2: Convert the number to string using the ToString() method and to pad the string use the formatted string argument “0000” for P= 4.
val = N.ToString("0000");Step 3: Return the padded string.
Example :
C#
// C# program to pad an integer // number with leading zeros using System; public class GFG{ // Function to pad an integer number // with leading zeros static string pad_an_int(int N, int P) { // string used in ToString() method string s = ""; for( int i=0 ; i<P ; i++) { s += "0"; } // use of ToString() method string value = N.ToString(s); // return output return value; } // driver code public static void Main(string[] args) { int N = 123; // Number to be pad int P = 5; // Number of leading zeros Console.WriteLine("Before Padding: " + N); // Function calling Console.WriteLine("After Padding: " + pad_an_int(N, P)); } } |
Output:
Before Padding: 123 After Padding: 00123
Method 3: Using PadLeft() method : The PadLeft() method is used to right-aligns the characters in String by padding them.
Step 1: Get the Number N and number of leading zeros P.
Step 2: Convert the number to string using the ToString() method.
val = N.ToString();Step 3: Then pad the string by using the PadLeft() method.
pad_str = val.PadLeft(P, '0');Step 4: Return the padded string.
Example :
C#
// C# program to pad an integer // number with leading zeros using System; public class GFG{ // Function to pad an integer number // with leading zeros static string pad_an_int(int N, int P) { // use of ToString() method string value = N.ToString(); // use of the PadLeft() method string pad_str = value.PadLeft(P, '0'); // return output return pad_str; } // driver code public static void Main(string[] args) { int N = 123; // Number to be pad int P = 5; // Number of leading zeros Console.WriteLine("Before Padding: " + N); // Function calling Console.WriteLine("After Padding: " + pad_an_int(N, P)); } } |
Output:
Before Padding: 123 After Padding: 00123
Recommended Posts:
- C# Program to Convert the Octal String to an Integer Number
- Different ways to convert String to Integer in C#
- Different Ways to Convert Double to Integer in C#
- How to Convert Integer Array to List in C#?
- C# Program to Convert a Binary String to an Integer
- C# Program for Converting Hexadecimal String to Integer
- C# | Get the number of elements actually contained in the ArrayList
- C# | Get the number of strings in StringCollection
- C# | Number of elements in HashSet
- C# | Get the number of elements contained in the Stack
- C# | Get the number of elements contained in the Queue
- C# | Count the number of key/value pairs in the Hashtable
- C# | Count the total number of elements in the List
- C# | Get the number of elements in the SortedSet
- C# | Get or set the number of elements that the ArrayList can contain
- C# | Get or set the number of elements in the BitArray
- C# | Number of elements contained in the BitArray
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Get the number of key/value pairs contained in ListDictionary
- C# | Count the number of key/value pairs in HybridDictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

