How to Pad an Integer Number With Leading Zeroes in C#?

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

Before Padding: 123
After Padding: 00123



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.