SQLite REAL数据类型值比较问题

SQLite REAL数据类型值比较问题

数据类型是REAL的字段,where条件where 字段 = 值查询时,可能查询结果为空,使用 where abs(字段 - 值) < 0.00001查询

例子

表结构

字段名称 数据类型 说明(可选)
ID INTEGER 序号
Name TEXT(50) 名称
Amount REAL 数量
CREATE TABLE [tCeShi](
  [ID] INTEGER, 
  [Name] TEXT(50), 
  [Amount] REAL);

表中数据

使用SQLite Expert从CSV文件中导入的

ID Name Amount
1 商品A 0.31
2 商品B 0.46
3 商品C 0.73
4 商品D 1.09
5 商品E 1.58
6 商品F 2.17
7 商品G 6.56
8 商品H 9.17
9 商品I 11.68
10 商品J 13.06
11 商品K 14.9

查询语句

Select * from tCeShi where Amount = 13.06

返回结果为空

使用printf进行打印

select printf("%10.20f", Amount) from tCeShi 

返回结果

printf("%10.20f", Amount)
13.05999999999998000000

修改查询语句

Select * from tCeShi where abs(Amount - 13.06) < 0.00001

返回结果

ID Name Amount
10 商品J 13.06

参考文档

https://stackoverflow.com/questions/58740993/real-values-floating-point-comparing-issue-in-sqlite

posted @ 2022-10-25 15:31  txgh  阅读(710)  评论(0编辑  收藏  举报