Real vs. Floating Point vs. Money [英] Real vs. Floating Point vs. Money

查看:30
本文介绍了Real vs. Floating Point vs. Money的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我在 SQL Server 中将 40.54 的值保存到 Real 类型的列时,它返回给我的值更像是 40.53999878999 而不是 40.54?我已经看过几次了,但一直不明白为什么会发生这种情况.有没有其他人遇到过这个问题,如果有,是导致它的原因吗?

Why when I save a value of say 40.54 in SQL Server to a column of type Real does it return to me a value that is more like 40.53999878999 instead of 40.54? I've seen this a few times but have never figured out quite why it happens. Has anyone else experienced this issue and if so causes it?

推荐答案

看看 每位计算机科学家都应该了解的浮点运算知识.

计算机中的浮点数不能准确地表示小数.相反,它们代表二进制分数.大多数小数没有作为二进制分数的精确表示,因此需要进行一些四舍五入.当这样一个四舍五入的二进制分数被转换回十进制分数时,你就会得到你所描述的效果.

Floating point numbers in computers don't represent decimal fractions exactly. Instead, they represent binary fractions. Most fractional numbers don't have an exact representation as a binary fraction, so there is some rounding going on. When such a rounded binary fraction is translated back to a decimal fraction, you get the effect you describe.

为了存储货币值,SQL 数据库通常提供一个 DECIMAL 类型来存储精确的十进制数字.这种格式对于计算机处理来说效率稍低,但是当您想避免小数舍入错误时,它非常有用.

For storing money values, SQL databases normally provide a DECIMAL type that stores exact decimal digits. This format is slightly less efficient for computers to deal with, but it is quite useful when you want to avoid decimal rounding errors.

这篇关于Real vs. Floating Point vs. Money的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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