Reputation: 2484
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)
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);
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.
Upvotes: 9
Views: 6250
Reputation: 138979
According to the official documentation regarding data types in Cloud Firestore, there are two numeric data types:
Floating-point number: 64-bit double precision, IEEE 754.
Integer: 64-bit, signed
And the solution to your problem:
My problem is really simple: I want to save Double values, but when a number is like 113 and not 145.3 for exemple, the value is automatically saved in Long.
If you want to save 113
as double, then save it as 113.3
not simply as 113
because Firestore will see that 113
is a simple number of type Integer
and it will save it accordingly. Always save the data according to the data type you need.
Edit:
Doesn't matter if you add, either 113
or 113.0
, the value is saved in the database as 113
. If you want to get it as 113.0
then you have to get it as a double
. In Android, you can use the following line of code:
double doubleValue = document.getDouble("myDoubleProperty");
Even if the myDoubleProperty
holds the Integer value of 113
, it can be saved into a double. A double can alsways hold an Integer while an Integer can never hold a double without a specific cast.
Upvotes: 5