8
\$\begingroup\$

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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 30, 2012 at 16:28
\$\endgroup\$

1 Answer 1

11
\$\begingroup\$

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

string taskNumber = string.Format("{0:D6}-{1:D4}", order.ID, task.ID);
answered Oct 30, 2012 at 16:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.