精度说明符作为参数,如printf但在String.Format中

Spr*_*tar 8 c# string printf string-formatting

使用printf我可以使用*将精度值指定为额外参数.C#String.Format中是否存在相同的功能?

编辑:例如:

Console.WriteLine("{0:D*}",1,4); // Outputs 0001
Run Code Online (Sandbox Code Playgroud)

Hei*_*nzi 4

不,String.Format 不支持星号运算符。您需要使用字符串连接

Console.WriteLine("{0:D" + myPrecision.ToString() + "}",1);
Run Code Online (Sandbox Code Playgroud)

或嵌套String.Formats

Console.WriteLine(String.Format("{{0:D{0}}}", 4), 1);
Run Code Online (Sandbox Code Playgroud)