C#的两个双数相加的问题 [英] c#: sum of two double numbers problem

查看:368
本文介绍了C#的两个双数相加的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
在C#中不精确为什么浮点运算?






喜。我有以下问题:

  43.65 + 61.11 = 104.75999999999999 

十进制是正确的:

 (十进制)43.65+(十进制)61.11 = 104.76 

为什么导致双错了?


< DIV CLASS =h2_lin>解决方案

这个问题和它的答案都在这丰富的信息的 - 的什么是小数之间的区别,在C#Float和Double?



引述:





  • 有关这是自然确切
    小数的价值观是很好用十进制。
    这是通常适用于由人类发明的任何
    概念:金融
    值是最明显的例子,
    ,但也有其他人了。考虑给予潜水员或滑冰,
    例如
    比分。


  • 有关这是
    更多的文物价值自然无法真正进行测量
    正好反正浮点/双精度更
    为宜。例如,科学
    数据通常可以在
    这种形式表示。在这里,原始值
    不会是十进制准确开始
    用,所以它不是为
    重要的预期结果维持
    精确到小数点。浮动二进制
    点类型快得多工作
    比小数。




Possible Duplicate:
Why is floating point arithmetic in C# imprecise?

Hi. I've got following problem:

43.65+61.11=104.75999999999999

for decimal is correct:

(decimal)43.65+(decimal)61.11=104.76

Why result for double is wrong?

解决方案

This question and its answers are a wealth of info on this - What is the difference between Decimal, Float and Double in C#?

To quote:

  • For values which are "naturally exact decimals" it's good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example.

  • For values which are more artefacts of nature which can't really be measured exactly anyway, float/double are more appropriate. For example, scientific data would usually be represented in this form. Here, the original values won't be "decimally accurate" to start with, so it's not important for the expected results to maintain the "decimal accuracy". Floating binary point types are much faster to work with than decimals.

这篇关于C#的两个双数相加的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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