是一个双重真的不适合钱吗? [英] Is a double really unsuitable for money?
问题描述
我总是告诉在c#double类型的变量是不适合的钱。所有奇怪的事情可能发生。但我似乎无法建立一个例子来说明其中的一些问题。任何人都可以提供这样的例子吗?
I always tell in c# a variable of type double is not suitable for money. All weird things could happen. But I can't seem to create an example to demonstrate some of these issues. Can anyone provide such an example?
(编辑;这个职位最初标记C#;一些答复提及小数
,因此这意味着<一的具体细节href=\"http://msdn.microsoft.com/en-us/library/system.decimal.aspx\"><$c$c>System.Decimal$c$c>).
(edit; this post was originally tagged C#; some replies refer to specific details of decimal
, which therefore means System.Decimal
).
(编辑2:我是具体要求一些C#code,所以我不认为这是语言无关只)
(edit 2: I was specific asking for some c# code, so I don't think this is language agnostic only)
推荐答案
非常,非常不合适。使用十进制。
Very, very unsuitable. Use decimal.
double x = 3.65, y = 0.05, z = 3.7;
Console.WriteLine((x + y) == z); // false
(例如,从乔恩的网页。这里 - 推荐阅读;-p)
(example from Jon's page here - recommended reading ;-p)
这篇关于是一个双重真的不适合钱吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!