android - 在 Android 下选择带有计算的列时精度丢失

标签 android sql ormlite

我在数据库中有不同的列。我使用 ormlite 并使用 genericRawResults 执行 SQL。我的代码是这样的:

GenericRawResults<Total> rawResults =
    dao.queryRaw("select items*3 from Items", new RawRowMapper<Total>() {
        public Total mapRow(String[] columnNames,
            String[] resultColumns) throws SQLException {
                   return new Total(new BigDecimal(resultColumns[0]));
        }
        });

结果是一个丢失小数点的数字。为什么?

我尝试使用 cast(columnname as REAL) 但结果是一样的。我能做什么?谢谢。

最佳答案

如果没有看到 Sqlite 模式,也没有看到 item*3String 结果,很难具体知道这里出了什么问题。

我怀疑这不是 ORMLite 的问题然而。我怀疑这是 Sqlite 的局限性,因为它使用小精度进行内部计算。下面是来自 Sqlite 命令行的示例:

select 11312541243231.01*3;
33937623729693.0  // << note the missing .03

我建议您不要在查询中执行*3,而是它已转换为BigDecimal。我猜是这样的:

BigDecimal big = new BigDecimal(resultColumns[0]);
big.multiply(new BigDecimal(3));

关于android - 在 Android 下选择带有计算的列时精度丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11014181/

相关文章:

javascript - 网页如何判断它是否在没有引荐来源网址的 webapp View 中打开?

java - 如何从firebase数据库中的内部节点读取值?

java - 安卓,SQL : how to find out if existing column has a value?

sql - 在sql中,多次使用子查询的最佳语法

java - 找不到 "avoid passing null as the view root"的父项

sql - 使用嵌套的 SELECT Access INSERT

android - ForeignCollection 未使用查询生成器加载

java - 使用从 Jackson 创建的对象在 Android 上使用 OrmLite 保存子集合

android - (Gradle和OrmLite配置)如何在Java编译之后但.apk生成之前添加资源文件?

android - 如何从数据库获取项目的值并将其设置为微调器值