十进制对双!- 我应该在什么时候使用哪一个? [英] decimal vs double! - Which one should I use and when?

查看:30
本文介绍了十进制对双!- 我应该在什么时候使用哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到人们在 C# 中使用双精度.我知道我在某处读到双打有时会失去精度.我的问题是什么时候应该使用双精度型,什么时候应该使用十进制类型?哪种类型适合货币计算?(即超过 1 亿美元)

I keep seeing people using doubles in C#. I know I read somewhere that doubles sometimes lose precision. My question is when should a use a double and when should I use a decimal type? Which type is suitable for money computations? (ie. greater than $100 million)

推荐答案

对于金钱,总是十进制.这就是它被创建的原因.

For money, always decimal. It's why it was created.

如果数字必须正确相加或平衡,请使用小数.这包括人们可能手动完成的任何财务存储或计算、分数或其他数字.

If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand.

如果数字的确切值不重要,请使用 double 以提高速度.这包括图形、物理或其他物理科学计算,其中已经有有效数字的数量".

If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other physical sciences computations where there is already a "number of significant digits".

这篇关于十进制对双!- 我应该在什么时候使用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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