Kev*_*inB 5 javascript double firebase google-cloud-firestore
I'm currently trying to save values on Firebase (on cloud Firestore).
My problem is really simple: I want to save Double values, but when a number is like 113 and not 145.3 for example, the value is automatically saved in Long.
This is a problem because my Android app crash because of that, and it's a mess to handle values that are Double and Long at the same time.
I tried:
parseFloat(15)
Run Code Online (Sandbox Code Playgroud)
This is not working, if I do a console.log(value) I got 15 (not Double I guess).
I tried also:
var number = 15;
var doubleValue = number.toFixed(2);
Run Code Online (Sandbox Code Playgroud)
This is not working, value is saved in String.
This appears to be much more complicated to handle this on the web than on iOS or Android.
根据有关Cloud Firestore 中数据类型的官方文档,有两种数字数据类型:
浮点数:64 位双精度,IEEE 754。
整数:64 位,有符号
以及解决您的问题的方法:
我的问题很简单:我想保存 Double 值,但是当一个数字类似于 113 而不是 145.3 时,该值会自动保存在 Long 中。
如果你想保存113为双精度,那么就113.3 不要简单地保存它,113因为 Firestore 会看到它113是一个简单的类型Integer,它会相应地保存它。始终根据您需要的数据类型保存数据。
编辑:
无论您添加113还是113.0,该值都将在数据库中保存为113. 如果你想得到它,113.0那么你必须把它作为double. 在 Android 中,您可以使用以下代码行:
double doubleValue = document.getDouble("myDoubleProperty");
Run Code Online (Sandbox Code Playgroud)
即使myDoubleProperty持有 的 Integer 值113,也可以将其保存为双精度值。double 总是可以保存一个 Integer 而一个 Integer 永远不能在没有特定转换的情况下保存一个 double。
| 归档时间: |
|
| 查看次数: |
3061 次 |
| 最近记录: |