使用Realm.io存储货币值 [英] Using Realm.io to store money values

查看:78
本文介绍了使用Realm.io存储货币值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在我正在编写的Android应用程序中使用Realm.io.在我的一个数据对象中,我需要存储货币值.以前,我在内部将值存储为BigDecimal值,然后在移入和移出数据库时也将其转换为double值.

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 value internally as a BigDecimal value and then converted that too and from a double value when moving in and out of the database.

一直有人告诉我,将货币值存储为双精度值是一个不好的主意,因为它们的处理方式. 不幸的是,Realm.io不支持BigDecimal对象的存储和检索.

I have always been told that it is a bad idea to store currency values in a double because of the way that they are handled. Unfortunately, Realm.io doesn't support the storage and retrieval of BigDecimal objects.

编写我自己的货币类(扩展RealmObject并将其保留为by数据对象的成员变量)的最佳解决方案?

Is the best solution to write my own currency class that extends RealmObject and keeps that as a member variable of by data object?

推荐答案

这里是Realm的Emanuele.

Emanuele from Realm here.

您是正确的,使用浮点数或双精度货币是一种想法.

You are right, using floats or doubles for currency is a bad idea.

我们暂时不支持BigDecimal,在此之前,我们必须先了解BigDecimal与所有其他语言绑定的关系,因为我们希望领域文件在所有受支持的平台上都兼容.

We don't support BigDecimal for now, and before we do we will have to see how that plays in relation to all other language bindings since we want realm files to be compatible across all the supported platforms.

Christian的想法很好,但是我发现往返于String的转换有些慢.如果不需要BigDecimal的任意精度属性,则可以使用long并乘以/除以所需精度要求的因子.由于整数值是按位打包的,因此在Realm文件的大小方面也可以节省很多空间.

Christian's idea is good, but I see the conversion to and from String to be a bit slow. If you don't need the arbitrary precision property of BigDecimal you could use long and multiply/divide by the factor your required precision calls for. This would also save a lot of space in terms of the size of the Realm file since integer values are bit packed.

这篇关于使用Realm.io存储货币值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆