leading 0's in a text string

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

I want to be able to convert my int to a 4 character string,

e.g. I wish to convert int 1 to string "0001"

How do I do this?

Thanks
 
Tony said:
I want to be able to convert my int to a 4 character string,

e.g. I wish to convert int 1 to string "0001"

How do I do this?

You need the appropriate format string, which in this case is "D4". For
instance:

string s = String.Format ("{0:D4}", 1);
or
string s = 1.ToString("D4");

(in both cases, s will be "0001").

If there are more than 4 digits in the integer, they will all be
present though, eg 12345.ToString("D4") returns "12345".
 
I want to be able to convert my int to a 4 character string,

e.g. I wish to convert int 1 to string "0001"

How do I do this?

Thanks

int i = 1;
string si = i.ToString("0000");


Sunny
 
Back
Top