How to pad a string with zero's so that "6" is "000006" [C# 2005]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
There are a bunch of places where I use a string value such as "6" or "10" where I want to display it as "000006" or "000010" (6 characters, 0 padded).
Within a GridView [asp.net 2.0] this is easy as I simply use the DataFormatString {0:000000}, but how can this be achieved for a normal string that I use to populate a DrillDown or Label witin a .cs file? How can I pad a string with "0"'s so that it is always X characters long?

Any help would be greatly appreciated...
Thanks,
 
string.Format

If you wish to perform the conversion from a numeric data type to a padded string with one operation:

C#:
string str = string.Format("{0:d6}", number);

Good luck :cool:
 
Back
Top