gpt4 book ai didi

java - SQLiteDB 中大量数字丢失精度?

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:53 26 4
gpt4 key购买 nike

我将 0.00 个十进制值作为文本存储在 SQLiteDB 中。我正在通过以下方式检索它们

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

问题是当数字开始达到 10000.00 时,精度似乎出现问题。

最佳答案

该值已转换为 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

Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes: NULL, INTEGER, REAL, TEXT, BLOB

What you need to know about Floating Point maths

关于java - SQLiteDB 中大量数字丢失精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5573920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com