Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm writing out two numbers separated with a dash. The first number is padded with leading zeros until 6 digits, the second number, 4.

string taskNumber = order.ID.ToString("D6") + "-" + task.ID.ToString("D4");

If I was going to rewrite this using string.Format I would simply say:

string taskNumber = string.Format("{0}-{1}", order.ID.ToString("D6"), task.ID.ToString("D4"));

Is there anything I can do with string.Format's {0} and {1} to say that I want my numbers padded? Calling ToString is a bit verbose, IMO.

share|improve this question

1 Answer 1

up vote 10 down vote accepted

Sure. Simply include the padding specifier directly in the format string:

string taskNumber = string.Format("{0:D6}-{1:D4}", order.ID, task.ID);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.