-2

I think that I don't understant ToString() method correctly. How to get number on 3 digits?

12 as 012
123 as 123
6 as 006

i.ToString("000");

results in 6 as 600.

Termininja
  • 6,620
  • 12
  • 48
  • 49
  • 4
    `PadLeft` with '0', this should do the trick. Documentation is available here https://msdn.microsoft.com/en-us/library/92h5dc07%28v=vs.110%29.aspx – Alex Dec 22 '15 at 09:05
  • Use String.format, http://www.dotnetperls.com/format – AnOldSoul Dec 22 '15 at 09:07
  • 1
    If i is 6, i.ToString("000") gave 006 when I tested. – Adil Dec 22 '15 at 09:07
  • 1
    Use d. then you don't need to write to many zeros. – Ian Dec 22 '15 at 09:10
  • @Adil Point for you, but I have no idea why. I've used d3 from below, getting error ("wrong format"), and then switched back to 000. Works now, but I still wonder what was the reason. – Michał Woliński Dec 22 '15 at 09:24

2 Answers2

1

You should use d. For example: ToString("d3");

Liam
  • 27,717
  • 28
  • 128
  • 190
Ian
  • 30,182
  • 19
  • 69
  • 107
0
i.PadLeft(3,'0');

More info on PadLeft https://msdn.microsoft.com/en-us/library/92h5dc07(v=vs.110).aspx

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32