Choose one of the following ways for padding a number with zeros:
Note that a number can be converted to string via ToString().
1. using the Format method of string
Code:string strTemp = String.Format("{0:0000}", aStr);
2. using PadLeft() or PadRigh() method of string
Code:aStr.PadLeft(nTotalWidth, '0');
Related:
Padding with spaces
Code:aStr.PadLeft(nTotalWidth);
Padding right
Code:aStr.PadRight(nTotalWidth);
3. classic way
Code: strTemp = "0000" + aStr;
strTemp = strTemp.Substring(strTemp.Length - 4);
4. More on string formatting
http://blog.stevex.net/string-formatting-in-csharp/