小编Jow*_*ows的帖子

是否可以在转义的花括号之间使用字符串插值格式复合格式?

我想在转义的花括号("{{"和"}}"之间构建具有格式化值的字符串.

我更喜欢使用格式字符串而不是ToString()方法来格式化值.

    //Works fine but don't use composite format string
    $"{{{Math.PI.ToString("n2")}}}" // return {3.14}

    //Use composite format string but does not work
    $"{{{Math.PI:n2}}} // return {n2}

    //Use composite format string but does not work
    $"{{{null:n2}}} // return {

    //Use composite format string, work fine but I do not want extra space
    $"{{{Math.PI:n2} }} // return {3.14 }    
Run Code Online (Sandbox Code Playgroud)

c# string-interpolation format-string

5
推荐指数
1
解决办法
92
查看次数

标签 统计

c# ×1

format-string ×1

string-interpolation ×1