C# convert int to string with padding zeros?

前端 未结 13 2006

In C# I have an integer value which need to be convereted to string but it needs to add zeros before:

For Example:

int i = 1;

When

13条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 07:19

    int p = 3; // fixed length padding
    int n = 55; // number to test
    
    string t = n.ToString("D" + p); // magic     
    
    Console.WriteLine("Hello, world! >> {0}", t);
    
    // outputs: 
    // Hello, world! >> 055
    

提交回复
热议问题