大数字在SQLiteDB中失去了精度?

时间:2011-04-06 22:52:57

标签: java sql

我将0.00十进制值存储为SQLiteDB中的文本。我正在通过

检索它们
"SELECT SUM(SUBSTR(amount, 1)) FROM record WHERE date BETWEEN '" +
    date1 + "' AND '" + date2 + "'";

问题是当数字开始上升到10000.00时,似乎存在精确度问题。

1 个答案:

答案 0 :(得分:1)

该值已转换为REAL(具有浮点问题)。一种选择就是围绕它

"SELECT ROUND(SUM(SUBSTR(amount, 1)),2) FROM record WHERE date BETWEEN '"+date+"' AND '"+date2+"'";

或者使用INTEGER数学,将位置移动2位小数

"SELECT SUM(CAST(SUBSTR(amount, 1)*100 as INT))/100.0 FROM record WHERE date BETWEEN '"+date+"' AND '"+date2+"'";

http://www.sqlite.org/datatype3.html

  

存储在SQLite数据库中(或由数据库引擎操纵)的每个值都具有以下存储类之一: NULL,INTEGER,REAL,TEXT,BLOB

What you need to know about Floating Point maths