423,498 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,498 IT Pros & Developers. It's quick & easy.

String Format padding with zeros

P: n/a
RSH

I have strings that are being converted that need to be eight characters
long. The values are coming in as anywhere between 1 and 8 characters long.
I need to pad the strings with zeros to make all IDs 8 characters long:

Example

Input number Need
1 00000001
121 00000121
10567 00010567
I tried this but it doesn not work, I simply get the same ID back (Example:
2)

sTemp = String.Format("{0:########}", DR["ID"].ToString());

What am I missing?

Thanks,

Ron
Jan 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

RSH wrote:
I have strings that are being converted that need to be eight characters
long. The values are coming in as anywhere between 1 and 8 characters long.
I need to pad the strings with zeros to make all IDs 8 characters long:

Example

Input number Need
1 00000001
121 00000121
10567 00010567
I tried this but it doesn not work, I simply get the same ID back (Example:
2)

sTemp = String.Format("{0:########}", DR["ID"].ToString());

What am I missing?


sTemp = DR["ID"].ToString().PadLeft(8, "0"c);

--
Larry Lard
Replies to group please

Jan 10 '06 #2

P: n/a
I always find the number formatting stuff difficult to find in the
documentation, but if you start with String.Format you can eventually
find it here:

http://msdn.microsoft.com/library/de...matstrings.asp

The answer to your question appears to be this format string:

sTemp = String.Format("{0:00000000}", DR["ID"].ToString());

Jan 10 '06 #3

P: n/a

Larry Lard wrote:
RSH wrote:
I have strings that are being converted that need to be eight characters
long. The values are coming in as anywhere between 1 and 8 characters long.
I need to pad the strings with zeros to make all IDs 8 characters long:

Example

Input number Need
1 00000001
121 00000121
10567 00010567
I tried this but it doesn not work, I simply get the same ID back (Example:
2)

sTemp = String.Format("{0:########}", DR["ID"].ToString());

What am I missing?


sTemp = DR["ID"].ToString().PadLeft(8, "0"c);


Which is some hellish mix of C# and VB.NET syntax, sorry. I meant of
course

sTemp = DR["ID"].ToString().PadLeft(8, '0');

--
Larry Lard
Replies to group please

Jan 10 '06 #4

P: n/a
Sorry. I didn't notice that ToString() there.

Assuming that DR["ID"] is a number, not a string, then the code should
be this:

sTemp = String.Format("{0:00000000}", DR["ID"]);

Jan 10 '06 #5

P: n/a
RSH
Thanks! Perfect.

I appreciate the quick response.

Ron
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Larry Lard wrote:
RSH wrote:
> I have strings that are being converted that need to be eight
> characters
> long. The values are coming in as anywhere between 1 and 8 characters
> long.
> I need to pad the strings with zeros to make all IDs 8 characters long:
>
> Example
>
> Input number Need
> 1 00000001
> 121 00000121
> 10567 00010567
>
>
> I tried this but it doesn not work, I simply get the same ID back
> (Example:
> 2)
>
> sTemp = String.Format("{0:########}", DR["ID"].ToString());
>
>
>
> What am I missing?


sTemp = DR["ID"].ToString().PadLeft(8, "0"c);


Which is some hellish mix of C# and VB.NET syntax, sorry. I meant of
course

sTemp = DR["ID"].ToString().PadLeft(8, '0');

--
Larry Lard
Replies to group please

Jan 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.