-1

I know I can format string with left padding with spaces with line:

int i=5;
serial = String.Format("{0,20}", i);

But how to left pad wit 0 in order to get string below?

00000000000000000005
vico
  • 17,051
  • 45
  • 159
  • 315

1 Answers1

2

You can use the built-in function PadLeft:

int i = 5;
var result = i.ToString().PadLeft(8, '0');

Note: this doesn't work for negative numbers (you'd get 00000000-5).

For that you can use the built-in formatters:

int i = 5;
var result = string.Format("{0:D8}", i);
Kenneth
  • 28,294
  • 6
  • 61
  • 84