Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted

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,

Posted

string.Format

 

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

 

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

 

Good luck :cool:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...