C# 打印双值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14077590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:36:46  来源:igfitidea点击:

Printing double values

c#

提问by dharmatech

On my system, the following code prints '3.6':

在我的系统上,以下代码打印“3.6”:

double a = 1.2;
int b = 3;

double c = a * b;

Console.WriteLine(c);

But in the debugger, I can see that chas a value with more than 2 digits:

但是在调试器中,我可以看到c有一个多于 2 位的值:

enter image description here

在此处输入图片说明

I know that I can display the full representation with Console.WriteLine("{0:R}", c). Is this the only and recommended way to display the actual value of a double?

我知道我可以用Console.WriteLine("{0:R}", c). 这是显示 a 的实际值的唯一且推荐的方法double吗?



update

更新

Going with the above example, I'd like to print csuch that if the user were to take the printed value and insert that back into the code in a test using ==, the comparison would be true. In this case c == 3.5999999999999996returns true.

按照上面的例子,我想打印c,如果用户要使用打印的值并将其插入到测试中的代码中==,则比较结果为真。在这种情况下c == 3.5999999999999996返回 true。

回答by John

double a = 1.2;
int b = 3;

double c = a * b;
string formatted = c.ToString("N5");
Console.WriteLine(formatted);

回答by Tim Schmelter

Console.WriteLinecalls Double.ToStringwhich uses the the "G" format specifier. This uses the current culture to determine the number of decimal places (1 for "en-US").

Console.WriteLine呼叫Double.ToString其使用的“G”格式说明。这使用当前区域性来确定小数位数(“en-US”为 1)。

If you want to display 8 decimal places you can use the numeric format specifier:

如果要显示 8 位小数,可以使用数字格式说明符

Console.WriteLine(c.ToString("N8"));

Standard Numeric Format Strings

标准数字格式字符串

Edit: The debugger uses this method to convert a double to a string:

编辑:调试器使用此方法将双精度转换为字符串:

_ecvt_s

_ecvt_s

I assume it's the cheapest way to convert it.

我认为这是转换它的最便宜的方式。

Where i have found it: How does Visual Studio display a System.Double during debugging?

我在哪里找到它:Visual Studio 如何在调试期间显示 System.Double?

回答by MethodMan

You could also use a different approach for example if you want to return 2 decimal places you could try something like this

您也可以使用不同的方法,例如,如果您想返回 2 个小数位,您可以尝试这样的操作

double a = 1.2;
int b = 3;
double c = a * b;
var s = string.Format("{0:0.00}", c);
Console.WriteLine(s);

Output = 3.60

输出 = 3.60

if you want to suppress the last 0 where out put is 3.6 you could do

如果您想抑制输出为 3.6 的最后一个 0,您可以这样做

var s = string.Format("{0:0.##}", c);

Output = 3.6 feel free to play around with it

输出 = 3.6 随意使用它

回答by Eric Lippert

3.999999999999996 is not the actual value of the double either; that's just the value rounded off to fifteen places or whatever. There is no built-in way to display the actual exactvalue that the double is representing. This is really too bad, because every normal double can be represented exactly as a decimal string, and it would be nice to be able to see that.

3.999999999999996 也不是 double 的实际值;这只是四舍五入到十五位之类的值。没有内置方法来显示double 所代表的实际确切值。这真的太糟糕了,因为每个普通的 double 都可以精确地表示为一个十进制字符串,如果能够看到这一点就好了。

As a public service, I've put source code for a device which does that on my blog:

作为一项公共服务,我在我的博客上放置了一个设备的源代码:

http://ericlippert.com/2011/02/17/looking-inside-a-double/

http://ericlippert.com/2011/02/17/looking-inside-a-double/

Note that it uses the Rational class from Microsoft Solver Foundation. If you don't have that then you can either download it for free, or write your own Rational class; it's character-building to do so.

请注意,它使用来自 Microsoft Solver Foundation 的 Rational 类。如果您没有,那么您可以免费下载它,或者编写您自己的 Rational 类;这样做是为了塑造性格。

If the subject of how doubles work internally interests you, consider checking out my archive of handy articles explaining all that. It's at:

如果您对双打内部如何工作的主题感兴趣,请考虑查看我解释所有这些的方便文章档案。它在:

http://blogs.msdn.com/b/ericlippert/archive/tags/floating+point+arithmetic/

http://blogs.msdn.com/b/ericlippert/archive/tags/floating+point+arithmetic/

Start from the bottom; those are in reverse-chronological order.

从底部开始;这些是按时间倒序排列的。