问题描述
我要显示在报表中的数字,但是我只想表现出任何小数点,如果他们现在和我只想显示1位小数的空间。
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.
例如:如果数字是12,然后我要显示12
e.g. if the number is 12 then I want to show 12
如果数量为12.1,然后我要显示12.1
If the number is 12.1 then I want to show 12.1
如果数量为12.11那么我想显示12.1
If the number is 12.11 then I want to show 12.1
推荐答案
我前一阵子有一个非常类似的问题和答案是数字转换为字符串时使用的格式字符串。我有一个博客帖子涵盖本()
为您解决问题的方法是使用0的自定义数字格式字符串。 #
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. I have a blog post covering this (http://www.j-nunn.com/MyBlog/tabid/57/EntryId/31/Formatting-decimal-data.aspx)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;
Console.WriteLine(x.ToString("0.#"));
Console.WriteLine(y.ToString("0.#"));
Console.WriteLine(z.ToString("0.#"));
会给你以下的输出:
Will give you the following output:
12
12.1
12.1
这篇关于显示数字没有小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!