user226305 user226305 - 3 months ago 17
C# Question

Displaying numbers without decimal points

I want to display a number in a report, however I only want to show any decimal points if they are present and the I only want to show 1 decimal space.

e.g. if the number is 12 then I want to show 12

If the number is 12.1 then I want to show 12.1

If the number is 12.11 then I want to show 12.1


I had a very similar problem a while ago and the answer is to use a format string when converting the number to a string. The way to solve your issue is to use a custom numeric format string of "0.#"

double x = 12;
double y = 12.1;
double z = 12.11;

Will give you the following output: