Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Firestore - Save Double value to Firebase

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.

like image 226
KevinB Avatar asked Jul 04 '19 09:07

KevinB


People also ask

How do I stop duplicate files in firestore?

The best way to prevent duplicate nodes in firebase realtime database or duplicate documents in firebase firestore database is to keep a check in the app itself to verify that the record to be inserted doesn't already exist in the database by querying for data using key field in where clause.

Can you use firebase and firestore together?

You can use both Firebase Realtime Database and Cloud Firestore in your app, and leverage each database solution's benefits to fit your needs. For example, you might want to leverage Realtime Database's support for presence, as outlined in Build Presence in Cloud Firestore.

Why are my firestore reads so high?

If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads.

Can firebase store float?

Firebase can store any valid JavaScript number value, which covers much more than the likely range of BMI values. So if you want, you can just store the float values in the database directly.


1 Answers

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.

like image 154
Alex Mamo Avatar answered Sep 18 '22 17:09

Alex Mamo