C# convert int to string with padding zeros?

前端 未结 13 1945

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

    To pad int i to match the string length of int x, when both can be negative:

    i.ToString().PadLeft((int)Math.Log10(Math.Abs(x < 0 ? x * 10 : x)) + 1, '0')
    

提交回复
热议问题