C# convert int to string with padding zeros?

前端 未结 13 1788

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:09

    .NET has an easy function to do that in the String class. Just use:

    .ToString().PadLeft(4, '0')  // that will fill your number with 0 on the left, up to 4 length
    
    int i = 1; 
    i.toString().PadLeft(4,'0')  // will return "0001"  
    

提交回复
热议问题