C# – Integer formatting, padding to a given length


I need to pad the output of an integer to a given length.

For example, with a length of 4 digits, the output of the integer 4 is "0004" instead of "4". How can I do this in C# 2.0?

Best Solution

Use the string.Format command.

output = String.Format("{0:0000}", intVariable); 

More details: http://msdn.microsoft.com/en-us/library/fht0f5be.aspx