C# 字符串格式为一个十进制数

C# 字符串格式为一个十进制数,c#,asp.net,vb.net,C#,Asp.net,Vb.net,我有下面的数字。我想在小数点后显示一位数字。如何格式化 2.85 2 1.99 我使用了(“{0:0.0}”),但是数据显示 2.9 //It should be 2.8 2.0 //It should be 2 2.0 //It should be 1.9 尝试使用“{0:0.#}”作为格式字符串。但是,这只会修复.0。要修复舍入以始终向下舍入,您可能需要使用: string s = (Math.Floor(value * 10) / 10).ToString("0.#"); (严格的四

我有下面的数字。我想在小数点后显示一位数字。如何格式化

2.85
2
1.99
我使用了(“{0:0.0}”),但是数据显示

2.9 //It should be 2.8
2.0 //It should be 2
2.0 //It should be 1.9
尝试使用
“{0:0.#}”
作为格式字符串。但是,这只会修复
.0
。要修复舍入以始终向下舍入,您可能需要使用:

string s = (Math.Floor(value * 10) / 10).ToString("0.#");

(严格的四舍五入很不寻常)
Decimal[] decimals = { new Decimal(2.85), new Decimal(2), new Decimal(1.99) };

foreach (var x in decimals)
{
  Console.WriteLine(string.Format("{0:0.#}", Decimal.Truncate(x * 10) / 10));
}

// output
2.8
2
1.9