Question

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
Was it helpful?

Solution

when int x use

 x.ToString("00");
 String.Format("{0:00}",x);

OTHER TIPS

string o(int x)
{
    return string.Format("{0:00}", x);
}

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

string o(int x) {
    return x.ToString().PadLeft(2, '0');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top