0

Let's say I have an integer i, and want to display n characters total when I output the integer as a string (padding the string with leading zeros).

For example:

i = 10, n = 2 ==> 10
i = 10, n = 3 ==> 010
i = 5,  n = 2 ==> 05
i = 5,  n = 3 ==> 005
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Doug
  • 5,116
  • 10
  • 33
  • 42
  • @Grant Winney I would argue that it's not exactly a duplicate, since the question linked doesn't cover the padding being of variable length. – Tom Studee Jun 13 '14 at 02:09

1 Answers1

3
i.ToString(string.Format("D{0}", n));
Tom Studee
  • 10,316
  • 4
  • 38
  • 42