문제

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
도움이 되었습니까?

해결책

when int x use

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

다른 팁

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');
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top