views:

965

answers:

3

Hi, I have a requirement to pad all single digits numbers with a starting zero. Can some one please suggest the best method? (ex 1 -> 01, 2 -> 02, etc)

+6  A: 
number.ToString().PadLeft(2, '0')
Mr. Brownstone
+16  A: 

I'd call .ToString on the numbers, providing a format string which requires two digits, as below:

int number = 1;
string paddedNumber = number.ToString("00");
bdukes
Awesome, thank you!
Alex
+1  A: 

Assuming you're just outputing these values, not storing them

int number = 1;
Console.Writeline("{0:00}", number);

Here's a useful resource for all formats supported by .Net.

MrTelly