简体   繁体   中英

How do I format Int32 numbers?

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM