One decimal for string format

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
+5
source share
2 answers

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:

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

All Articles