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 //It 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With