Try the below method
private string AddLeadingZeros(int totalLength, string originalNumber)
{
string leadingZeros = string.Empty;
int counter = 0;
try
{
counter = totalLength - originalNumber.Length;
for (int leadingZeroCount = 0; leadingZeroCount < counter; leadingZeroCount++)
{
leadingZeros += "0";
}
originalNumber = leadingZeros + originalNumber;
}
catch (Exception ex)
{ LogExceptionDetails(ex); }
return originalNumber;
}
In the method you will need to pass two parameters the first one is the total length and the second one is the number. Basing on the difference of lengths it will calculate the number of zero's to be added to the string.
For example let us consider that the total length is "4" and the original string which you are passing is "1". Now calculate the difference that is total length - length of the original string (4 - 1) then it will add three zero's to the original string. If you consider your original value as "01" the it will add two zero's and so on.