android - 如何使用 ObjectBox 存储货币值 (BigDecimal)?

标签 android objectbox

为了表示货币值(value),我使用 BigDecimal 是因为其准确性( double 类型会导致错误)。那么,如何在 ObjectBox 中存储 BigDecimal 值,我应该使用什么类型的字段或转换器?

最佳答案

BigDecimal 没有默认支持,因此您必须创建 Converter。这是一个例子:

public class BigDecimalConverter implements PropertyConverter<BigDecimal, String> {

    @Override
    public BigDecimal convertToEntityProperty(String databaseValue) {
        return new BigDecimal(databaseValue);
    }

    @Override
    public String convertToDatabaseValue(BigDecimal entityProperty) {
        return entityProperty.toString();
    }
}

@Entity
public class BigDecimalEntity {

    @Convert(dbType = String.class, converter = BigDecimalConverter.class)
    private BigDecimal decimal;

    public BigDecimal getDecimal() {
        return decimal;
    }

    public void setDecimal(BigDecimal decimal) {
        this.decimal = decimal;
    }
}

关于android - 如何使用 ObjectBox 存储货币值 (BigDecimal)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277927/

相关文章:

java - SQLite查询产生最后一个但一行值而不是最后插入的行值

android - 带有 NDK grabCut 的 Android Studio 中的 OpenCV 始终返回黑色掩码

android - 如何设置 PropertyAnimation 的帧速率?

android - 确保从协程返回的值覆盖 android kotlin 中的默认值

java - Retrofit 2.0怎么删除?

Android 没有通过 serverSocket.accept();

android - 无法解析符号 MyObjectBox

android - 仅维护对象框中预定义的有限数量的条目

java - 使用 ObjectBox 存储一对多关系列表?

flutter - 对象盒 Dart : how to filter based on a ToOne relation?