我想使用带有{$100.00}
的string.Format(fmt, x)
语句在花括号(示例x=100
)中输出格式化的数字。
{
var x = 100M;
// works fine without a format specifier
string.Format("{{{0}}}", x); // "{100.00}"
// fails with a format specifier
string.Format("{{{0:C}}}", x); // "{C}"
// works with spaces
string.Format("{{ {0:C} }}", x); // "{ $100.00 }"
}
因此,在不使用诸如
{$100}
之类的笨拙解决方案的情况下,我应该在上面使用哪种格式的字符串来获取string.Format("{0}{1:C}{2}", "{", x, "}");
最佳答案
尝试使用以下代码:
string.Format("{{{0}}}", x.ToString("C"))
关于c# - 带花括号的字符串格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25312311/