3

I currently convert an integer to a string for display to the screen, but wish to maintain 4 digits - i.e 10 = "0010". What is the best way of going from an integer of 10 to a string of "0010"?

This is in C# and .NET 4.

Cheers!

Charles
  • 50,943
  • 13
  • 104
  • 142
CdrTomalak
  • 479
  • 1
  • 4
  • 15

3 Answers3

9
int num = 1;
// pad with 0 up to 4 places
string str = num.ToString("D4");
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
7
var str = 10.ToString("0000");
L.B
  • 114,136
  • 19
  • 178
  • 224
3

You can try :-

 string str = num.ToString("D8"); //padding with 8 zero's

Check this link

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331