I have entity:
@Entity(tableName = "products")
public class Product{
@PrimaryKey(autoGenerate = true)
private Long id;
@ColumnInfo(name = "amount")
private BigDecimal amount;
I need store it in Room DB. I Can not store BigDecimal
and I create converter:
public static class Converters {
@TypeConverter
public BigDecimal fromString(String value) {
return value == null ? null : new BigDecimal(value);
}
@TypeConverter
public Double amountToString(BigDecimal bigDecimal) {
if (bigDecimal == null) {
return null;
} else {
return bigDecimal.doubleValue();
}
}
}
and add to column:
@TypeConverters(Converters.class)
@ColumnInfo(name = "amount")
private BigDecimal amount;
Now I store currency in double column. And I want method for summ totalcurrency:
@Query("SELECT SUM(amount) FROM products")
LiveData<Double> totalSum();
But I think it is bad way, because I can lose some values when converting.
My question: How can I ctore currency in ROOM DB? and return method
LiveData<BigDecimal> totalSum();