是一个双重真的不适合钱吗? [英] Is a double really unsuitable for money?

查看:117
本文介绍了是一个双重真的不适合钱吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是告诉在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).

(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆