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#
using System;
class GFG{
static string pad_an_int( int N, int P)
{
string s = "{0:" ;
for ( int i=0 ; i<P ; i++)
{
s += "0" ;
}
s += "}" ;
string value = string .Format(s, N);
return value;
}
public static void Main( string [] args)
{
int N = 123;
int P = 5;
Console.WriteLine( "Before Padding: " + N);
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#
using System;
public class GFG{
static string pad_an_int( int N, int P)
{
string s = "" ;
for ( int i=0 ; i<P ; i++)
{
s += "0" ;
}
string value = N.ToString(s);
return value;
}
public static void Main( string [] args)
{
int N = 123;
int P = 5;
Console.WriteLine( "Before Padding: " + N);
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#
using System;
public class GFG{
static string pad_an_int( int N, int P)
{
string value = N.ToString();
string pad_str = value.PadLeft(P, '0' );
return pad_str;
}
public static void Main( string [] args)
{
int N = 123;
int P = 5;
Console.WriteLine( "Before Padding: " + N);
Console.WriteLine( "After Padding: " + pad_an_int(N, P));
}
}
|
Output:
Before Padding: 123
After Padding: 00123
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 May, 2020
Like Article
Save Article