Using Realm.io to store money values

前端 未结 4 1458
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 09:11

I\'m starting to play with Realm.io in an Android app that I\'m writing. In one of my data objects, I\'m required to store a currency value. Previously I had stored the valu

4条回答
  •  旧巷少年郎
    2021-01-18 09:57

    That could work, but would probably be suboptimal if do calculations on your current BigDecimal objects.

    You could also use the @Ignore annotation to provide a wrapper method for your custom objects like this:

    public class Money extends RealmObject {
    
      private String dbValue;
      @Ignore private BigDecimal value;
    
      public String getDbValue() {
          return dbValue;
      }
    
      public void setDbValue(String dbValue) {
          this.dbValue = dbValue;
      }
    
      public BigDecimal getValue() {
         return new BigDecimal(getDbValue());
      }
    
      public void setValue(BigDecimal value) {
          setDbValue(value.toString());
      }
    }
    

    It is not perfect as you need to expose the *dbValue() methods, but it should work.

    I would also suggest going to https://github.com/realm/realm-java/issues and make a feature request for this as BigDecimal is probably one of those java classes used by so many that it could warrant native Realm support, just like Date has.

提交回复
热议问题