String Format padding with zeros

  • Thread starter Thread starter RSH
  • Start date Start date
R

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
 
RSH said:
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 said:
RSH said:
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');
 
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"]);
 
Thanks! Perfect.

I appreciate the quick response.

Ron


Larry Lard said:
Larry said:
RSH said:
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');
 
Back
Top