GeneralChad GeneralChad - 3 years ago 96
C# Question

Using variable to define string.Format numeric format style

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.

Answer Source

This should work:

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

The double { and } is to escape curly braces.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download