views:

70

answers:

3

What is the best way to get formatted Int32 numbers?

Let say I have this o function:

string o(int x);

This is the value that o need to return according to x

x = 0    =>    o = 00
x = 1    =>    o = 01
x = 5    =>    o = 05
x = 10   =>    o = 10
x = 31   =>    o = 31
x = 106  =>    o = 106
+8  A: 

when int x use

 x.ToString("00");
 String.Format("{0:00}",x);
Axarydax
Wouldn't x.ToString( "D2" ) do this as well?
Morten Mertner
+2  A: 
string o(int x)
{
    return string.Format("{0:00}", x);
}
Darin Dimitrov
+1  A: 

Can use PadLeft to a total padding width of 2 with the character '0'.

string o(int x) {
    return x.ToString().PadLeft(2, '0');
}
John K