如何将数字格式化为指定位数的小数(保留尾随零),其中位数由变量指定?
例如:
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)
请注意,由于我想以编程方式控制位置的数量,所以这个string.Format无法工作(当然我不应该生成格式字符串):
Console.WriteLine(
string.Format("{0:0.000}", 1.2M)); // 1.200 (good)
我应该只包含Microsoft.VisualBasic并使用FormatNumber吗?
我希望我没有漏掉什么显而易见的东西。