C# convert int to string with padding zeros?

前端 未结 13 1775

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

    Here's a good example:

    int number = 1;
    //D4 = pad with 0000
    string outputValue = String.Format("{0:D4}", number);
    Console.WriteLine(outputValue);//Prints 0001
    //OR
    outputValue = number.ToString().PadLeft(4, '0');
    Console.WriteLine(outputValue);//Prints 0001 as well
    

提交回复
热议问题