Firebase - 存储货币类型值

时间:2017-06-19 13:43:31

标签: java python firebase firebase-realtime-database

我正在构建汇率应用并将Firebase用作数据存储区。 在以前的版本1上确实存储了费率'字符串中的数据,并在Android设备上将字符串转换为BigDecimal以进行计算。 现在,当我在第2版上工作时,我决定检查代码并进行更新。我找到了存储率的解决方案' Integer类型的数据比转换回Decimal / Double / Float。

流速:

后端Python - > dataTostore = int(货币汇率值* 100) Android Java - > rateValue = dataToStore / 100.00

有这个问题的经验吗?

如何解决它或使用字符串-BigDecimal足够好?

感谢。

1 个答案:

答案 0 :(得分:0)

Storing monetary values in counts of the smallest denomination is a common practice. It ensures that you can always count full values and reduces the risk of rounding errors associated with floating point types.

Whether an integer type is enough depends on the maximum value you want to store.

Firebase Database internally supports signed 64-bit longs (-2^63 ... 2^63-1). Note that JavaScript uses 64-bit double values internally so some large integers (greater than 2^53) are not precisely representable and will lose precision.