Convert int (number) to string with leading zeros? (4 digits) [duplicate]
Solution 1:
Use String.PadLeft like this:
var result = input.ToString().PadLeft(length, '0');
Solution 2:
Use the formatting options available to you, use the Decimal format string. It is far more flexible and requires little to no maintenance compared to direct string manipulation.
To get the string representation using at least 4 digits:
int length = 4;
int number = 50;
string asString = number.ToString("D" + length); //"0050"
Solution 3:
Use the ToString() method - standard and custom numeric format strings. Have a look at the MSDN article How to: Pad a Number with Leading Zeros.
string text = no.ToString("0000");