开发者

one decimal for string format

I have below digits. I want to show one digit after to decimal. How to format it?

2.85
2
1.99

I was using ("{0:0.0}". But data showing like

2.9 //I开发者_Python百科t should be 2.8
2.0 //It should be 2
2.0 //It should be 1.9


Try using "{0:0.#}" as the format string. However, that will only fix the .0. To fix the rounding to always round down, you might want to use:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜