.NET 中小数、浮点数和双精度数之间的区别? [英] Difference between decimal, float and double in .NET?

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

问题描述

.NET中decimalfloatdouble有什么区别?

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

何时有人会使用其中之一?

When would someone use one of these?

推荐答案

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

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浮动小数点输入.换句话说,它们代表一个这样的数字:

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

12345.65789

同样,decimal 点的数字和位置都编码在值中——这就是 decimal 仍然是浮点类型而不是定点类型的原因.

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.

需要注意的重要一点是,人类习惯于以十进制形式表示非整数,并期望以十进制表示的精确结果;并不是所有的十进制数都可以用二进制浮点数精确表示——例如 0.1——所以如果你使用二进制浮点值,你实际上会得到 0.1 的近似值.使用浮动小数点时,您仍然会得到近似值 - 例如,无法准确表示 1 除以 3 的结果.

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.

至于什么时候使用:

  • 对于自然精确小数"的值,最好使用 decimal.这通常适用于人类发明的任何概念:金融价值是最明显的例子,但也有其他的.例如,考虑给潜水员或滑冰者的分数.

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

对于更多是无法真正精确测量的自然人工制品的值,float/double 更多合适的.例如,科学数据通常以这种形式表示.在这里,原始值一开始就不会是十进制精度",因此对于预期结果来说,保持十进制精度"并不重要.浮点二进制小数点类型的处理速度比小数快得多.

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 中小数、浮点数和双精度数之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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