Open In App

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

Last Updated : 26 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

C# Program to Convert the Octal String to an Integer Number
Given an octal number as input, we need to write a program to convert the given octal number into equivalent integer. To convert an octal string to an integer, we have to use Convert.ToInt32() function to convert the values. Examples: Input : 202 Output : 130 Input : 660 Output : 432 Convert the item to an integer using base value 8 by using a fore
2 min read
Different ways to convert String to Integer in C#
Like other programming languages, in C# we can convert string to int. There are three ways to convert it and they are as follows: Using the Parse Method Using the TryParse Method Using the Convert Method from (System.Convert class) The input string can be anything like "10", "10.10", "10GeeksforGeeks", "" (Your string can be number, a combination o
4 min read
Different Ways to Convert Double to Integer in C#
Given a Double real number, the task is to convert it into Integer in C#. There are mainly 3 ways to convert Double to Integer as follows: Using Type CastingUsing Math.round()Using Decimal.ToInt32() Examples: Input: double = 3452.234 Output: 3452 Input: double = 98.23 Output: 98 1. Using Typecasting: This technique is a very simple and user friendl
2 min read
How to Convert Integer Array to List in C#?
We have given a integer array arr and the task is to convert integer array into the list lst in C#. In order to do this task, we have the following methods: Method 1: List&lt;T&gt; class: This represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List&lt;T&gt; class also provides the
5 min read
C# Program to Convert a Binary String to an Integer
Given an binary string as input, we need to write a program to convert the binary string into equivalent integer. To convert an binary string to integer, we have to use Convert.ToInt32(String, Base/Int32) function to convert the values. The base of the binary is 2. Syntax: Convert.ToInt32(String, Base/Int32); Examples: Input : 1010101010101010 Outp
2 min read
C# Program for Converting Hexadecimal String to Integer
Given an hexadecimal number as input, we need to write a program to convert the given hexadecimal number into equivalent integer. To convert an hexadecimal string to integer, we have to use Convert.ToInt32() function to convert the values. Syntax: Convert.ToInt32(input_string, Input_base); Here, input_string is the input containing hexadecimal numb
2 min read
C# Program to Find Binary Equivalent of an Integer using Recursion
Given an integer number, now we convert the given integer number into a binary number using recursion. Recursion is a method in which a function calls itself directly or indirectly and such type of function is known as a recursive function. It solves the problem very efficiently like we find the binary equivalent of an integer. Examples: Input : 10
3 min read
C# Program to Print the Numbers Greater Than 786 in an Integer Array using LINQ
Language-Integrated Query (LINQ) is a uniform query syntax in C# to retrieve data from different sources. It eliminates the mismatch between programming languages and databases and also provides a single querying interface for different types of data sources. In this article, we will learn how to display numbers greater than 786 in an array using L
2 min read
C# Program to Print Only Those Numbers Whose Value is Less Than Average of all Elements in an Integer Array using LINQ
Language-Integrated Query (LINQ) is a uniform query syntax in C# to retrieve data from different sources. It eliminates the mismatch between programming languages and databases and also provides a single querying interface for different types of data sources. In this article, we will learn how to print only those numbers whose value is less than th
2 min read
C# Program to Find Integer Numbers from the List of Objects and Sort them Using LINQ
Given a list of objects, we need to find the integer double numbers from the list of objects and sort them using LINQ. So this task can be done using the OfType() method and then the sorting of numbers can be done by using OrderBy() method. Let discuss them one by one along with their syntax: 1. OfType() method: It is used to filter the elements of
2 min read
Article Tags :