关于Float类型的比较的疑问

MySql 码拜 8年前 (2016-05-01) 862次浏览
本人数据库里有个字段version是用Float类型存储的,假设就一条记录,它的Float字段值为1.1.,
本人用SELECT * FROM dev_sqq_lightpos.tb_update where version > 1.1语句,为什么还能查出这条记录,不是应该查不出来吗,怎么样在数据库中比较小数?
解决方案

5

float 是用的浮点数,本身是不精确的。 可以百度一下 浮点数 以了解更多相关知识。

5

要精确,你应该用 decimal,  float和double存储的是近似数,不是精确值,所以略有差异是正常的

5

引用:
Quote: 引用:

要精确,你应该用 decimal,  float和double存储的是近似数,不是精确值,所以略有差异是正常的

本人换成decimal(5,5)了现在,结果现在想存储相似1和1.1这样的数,都存储不了
update dev_sqq_lightpos.tb_update set version = 1 where id = 1
直接报错说1264 out of range了,是一定要输入小数点5位数吗?但本人存储1.00000也存储不了啊

mysql默认float长度是不准确的,应该制定精度例如float(2,1)就回得到你要的结果

10

引用

M is the total number of digits (the precision) and D is the number of digits after the decimal point (the scale).

M是代表整数部分的位数” 没有这个说法,说明书上的说法为准。

15

For example, a DECIMAL(18,9) column has nine digits on either side of the decimal point, so the integer part and the fractional part each require 4 bytes. A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. The six fractional digits require 3 bytes.
— 看看官网文档的这个 sample, 你应该能够理解 decimal(5,5) 为啥存储不了1

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于Float类型的比较的疑问
喜欢 (0)
[1034331897@qq.com]
分享 (0)