c# - Displaying numbers without decimal points -
I want to display a number in a report, although I only want to show decimal numbers if they are present and I Want to show only 1 decimal place?
For example, if 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 would have 12.1
A short time ago I had a similar problem and the answer was to use a format string For when the number changes the string. The way to solve your problem is to use a custom numeric format string of "0 #"
double x = 12; Double y = 12.1; Double z = 12.11; Console.WriteLine (x.ToString ("0. #")); Console.WriteLine (y.ToString ("0. #")); Console.WriteLine (z.ToString ("0. #"));
gives you the following output:
12.1
12.1
Comments
Post a Comment