1
votes

I am trying to use custom TypeConverter for Android Room Persistance Library as shown in documentation. Main idea is to format all Sqlite SUM selects that Android Room Dao is preforming.

My code:

 @TypeConverter
public static double toDouble(String str) {
    return FormatHelper.formatAmount(Double.parseDouble(str));
}

@TypeConverter
public static String fromDouble(double doub) {
    return String.valueOf(FormatHelper.formatAmount(doub));
}

I already tried to clear data and rebuild the app. Converters for Date in the same class are working. Does the Android TypeConverters not supports Primitive variables?

EDIT:

I register TypeConverter in RoomDatabase class so it is used across whole database.

More specifically about my problem: if I have Query in my Dao class like this:

@Query("SELECT SUM(quantity) FROM production_plan_item")
Double getActivePlanItemsCount();

It may sometimes return value like 2.30000000000001.

I thought that Type converters are used to process returned value of queries. And every time before I save double to database or select double from it, my TypeConverter would round the double for me and I would not have to round it every time I select something.

Otherwise I have to round every single selected double manually.

1
"Main idea is to format all Sqlite SUM selects that Android Room Dao is preforming" -- that makes no sense to me. Formatting is not the responsibility of a persistence library. That is the responsibility of a presenter or some other piece of code associated with the user interface. Beyond that, where are you registering these TypeConverter methods? What are you expecting them to affect? - CommonsWare
@CommonsWare I edited my question. - Yoda066

1 Answers

3
votes

I thought that Type converters are used to process returned value of queries

Your @TypeConverter methods convert between double and String. SUM will not return a String. Your @Query method is returning a Double. Hence, neither of these @TypeConverter methods will be relevant, since there is no String<->double conversion taking place.

Otherwise I have to round every single selected double manually.

Perhaps you should be using fewer REAL columns in your database schema.