I have the numbers below. I want to show one digit after the decimal. How to format it?
2.85 2 1.99
I used ("{0: 0.0}". But the data showing how
2.9 //It should be 2.8 2.0 //It should be 2 2.0 //It should be 1.9
Try using "{0:0.#}"as a format string. However, this will fix only .0. To fix rounding so that it always rounds, you can use:
"{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