.NET中的decimal,float和double之间的区别? [英] Difference between decimal, float and double in .NET?

查看:185
本文介绍了.NET中的decimal,float和double之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

decimal float double in .NET?



什么时候有人会使用这些?

解决方案>

float double 浮动二进制点类型。换句话说,它们代表一个这样的数字:

  10001.10010110011 

二进制数的二进制数和二进制位的位置均被编码在值中。



decimal 浮动小数点点键入。换句话说,它们代表一个这样的数字:

  12345.65789 

再次,十进制点的数字和位置都在值内进行编码,这就是使 decimal 仍然是一个浮点类型,而不是一个固定点类型。



需要注意的重要事情是人类用于表示非整数一个十进制格式,并期望十进制表示中的精确结果;不是所有的十进制数都可以在二进制浮点数0.1中得到准确的表示 - 所以如果你使用一个二进制浮点值,你实际上会得到一个0.1的近似值。使用浮点数小数点时,仍然会得到近似值,例如,除以1除以3的结果。



至于什么使用时间:




  • 对于自然精确小数的值,使用十进制是很好的。这通常适合于人类发明的任何概念:财务价值观是最明显的例子,但也有其他的概念。考虑给潜水员或滑冰运动员的分数。


  • 对于那些不能真正被测量的自然物品的价值观无论如何, float / double 更合适。例如,科学数据通常以这种形式表示。在这里,原始值不会以十分准确开始,因此对于预期的结果来说,保持十进制精度并不重要。浮动二进制点类型比使用小数点更快。



What is the difference between decimal, float and double in .NET?

When would someone use one of these?

解决方案

float and double are floating binary point types. In other words, they represent a number like this:

10001.10010110011

The binary number and the location of the binary point are both encoded within the value.

decimal is a floating decimal point type. In other words, they represent a number like this:

12345.65789

Again, the number and the location of the decimal point are both encoded within the value – that's what makes decimal still a floating point type instead of a fixed point type.

The important thing to note is that humans are used to representing non-integers in a decimal form, and expect exact results in decimal representations; not all decimal numbers are exactly representable in binary floating point – 0.1, for example – so if you use a binary floating point value you'll actually get an approximation to 0.1. You'll still get approximations when using a floating decimal point as well – the result of dividing 1 by 3 can't be exactly represented, for example.

As for what to use when:

  • 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.

这篇关于.NET中的decimal,float和double之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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