Quick ToString() Question

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!
 
J

Joe Gass

You are looking for string.padleft
be something like...
yourInt32Var.toString.PadLeft(7,"0")
 
M

Munsifali Rashid

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
 
M

Mark Fox

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







.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top