2010-11-30 96 views
5

我有下面的数字。我想在十进制后显示一位数字。如何格式化?字符串格式的一位小数

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 
+1

(严格圆钢非常不寻常) – 2010-11-30 06:48:29

回答

7

尝试使用"{0:0.#}"作为格式字符串然而,这将只固定.0为了固定。四舍五入到总是本轮下跌,您可能需要使用:

string s = (Math.Floor(value * 10)/10).ToString("0.#"); 
2
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