Alex Alex - 1 year ago 138
C# Question

Format string to a 3 digit number

Instead of doing this, I wanted to make use of

to accomplish the same result:

if (myString.Length < 3)
myString = "00" + 3;

Answer Source

If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly:

myString = 3.ToString("000");

Or, alternatively, use the standard D format string:

myString = 3.ToString("D3");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download