1

I'd like to format a number using a run-time supplied format string. Isn't something like this possible?

string.Format("{0:{1}}",0,"c")

The "c" may change to be any other type of format string. I've tried various combinations but am failing to find the correct one.

1
  • you need a 2 step format. Step one to create the format string. Then step 2 to use that string in a second string.format to produce the final output Commented Jul 24, 2017 at 16:22

1 Answer 1

1

This should work:

var s0 = string.Format("{{0:{0}}}", "c");
var s1 = string.Format(s0, 0);

The double { and } is to escape curly braces.

Sign up to request clarification or add additional context in comments.

2 Comments

should that be var s0 = string.Format("{{0:{1}}}", 0, "c"); ?
This worked. string.Format(string.Format("between {{0:{2}}} and {{1:{2}}}", lowerBound, upperBound, formatString), lowerBound, upperBound)

Your Answer

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.