可能重复:
具有N个小数位数的Double.ToString
我想显示小数到6位小数,即使它包含6 x 0's例如:
3.000000
5.100000
3.456789
Run Code Online (Sandbox Code Playgroud)
等等,这可能吗?
如何将数字格式化为固定数量的小数位(保留尾随零),其中位数由变量指定?
例如
int x = 3;
Console.WriteLine(Math.Round(1.2345M, x)); // 1.234 (good)
Console.WriteLine(Math.Round(1M, x)); // 1 (would like 1.000)
Console.WriteLine(Math.Round(1.2M, x)); // 1.2 (would like 1.200)
Run Code Online (Sandbox Code Playgroud)
请注意,因为我想以编程方式控制位数,所以这个string.Format将不起作用(当然我不应该生成格式字符串):
Console.WriteLine(
string.Format("{0:0.000}", 1.2M)); // 1.200 (good)
Run Code Online (Sandbox Code Playgroud)
我应该只包含Microsoft.VisualBasic并使用FormatNumber吗?
我希望在这里遗漏一些明显的东西.