Quick ToString() Question

  • Thread starter Thread starter Mark Fox
  • Start date Start date
M

Mark Fox

Hello,

I have a number that I am looking to change into a
string, but when I do this it has to add zeros to make
the string long enough. I am looking to have the string
be seven chars long no matter what the number is (it's
never over 9999999):

int string
999 "0000999"
12343 "0012343"

What string would I pass to the Int32's ToString("")
method to do this? Thanks!
 
You are looking for string.padleft
be something like...
yourInt32Var.toString.PadLeft(7,"0")
 
The string object has a method called "PadLeft" which allows you to pad the
left side of a string with a specified unicode character.

So, you could use:

int number = 999;
string paddedNumber = number.ToString().PadLeft(7, Convert.ToChar("0"));

paddedNumber would then contain "0000999"

Hope this helps,

Mun
 
Thanks so much. That's what I was looking for!
-----Original Message-----
The string object has a method called "PadLeft" which allows you to pad the
left side of a string with a specified unicode character.

So, you could use:

int number = 999;
string paddedNumber = number.ToString().PadLeft(7, Convert.ToChar("0"));

paddedNumber would then contain "0000999"

Hope this helps,

Mun







.
 
Back
Top