FieldValue.increment produce the wrong sum when using decimals

441 views
Skip to first unread message

Robert Brunhage

unread,
May 19, 2019, 10:56:23 AM5/19/19
to Firebase Google Group
  1. Use FieldValue.increment to add 10.3 (double)
  2. Use FieldValue.increment to add 10.33 (double)
  3. this should be 20.63 but will produce 20.6300000000003


    Is this intended? Had this problem with Flutter so I tested out Cloud Functions and it produced the same sum.

Doug Stevenson

unread,
May 19, 2019, 5:26:36 PM5/19/19
to Firebase Google Group
Robert,

Are you familiar with the general difficulties that computers have with representing floating point values? If not, take a look at the section on accuracy problems of floating point values.  What you're observing is consistent with what you'd expect from standard floating point arithmetic.

If you require accurate floating point math, you'll definitely want to find a use a math library that does this for you.  On top of that, you will not want to use floating point type fields in Firestore (doc link), as it's stored in IEEE 754 format, which subject to the same inaccuracies.  You will probably want to find some other way to store the number, perhaps a string, or something that lets you separate the whole and fractional parts of the number.

Doug

Robert Brunhage

unread,
May 19, 2019, 10:10:26 PM5/19/19
to Firebase Google Group
Noticed that this has to do with floating point: https://floating-point-gui.de/basic/

What would be a good solution for this as I let the person add money which can have decimals and can be different currency types ex USD, EUR or SEK. I want to total this and for now I just round it in the app but feels so wrong to have a super long decimal value in the database.

Doug Stevenson

unread,
May 19, 2019, 10:23:11 PM5/19/19
to fireba...@googlegroups.com
The only thing I can recommend is doing some searches for how others have represented money as primitive fields in databases.  This is an old problem, and I'm sure there are standard ways of solving it.

Doug

--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/y3KFIELD4ag/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/5fce263c-78c9-4b63-8345-2135303e7c0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

hoang minh Tri

unread,
May 20, 2019, 11:00:14 AM5/20/19
to fireba...@googlegroups.com
Unfortunately, when I add the authentic dependencies of the fire base to my project, not only does the verification of the test phone number fail, it also causes other errors related to Google Place API. mine. Please show me how to remove my project from the firebase console without affecting Google Place API settings and Google map API. Firebase took me too many time and ruined my project

Vào Th 2, 20 thg 5, 2019 vào lúc 09:53 hoang minh Tri <hoangmi...@gmail.com> đã viết:
Disappointingly, when I added the firebase auth dependencies to my project, not only did the run test phone number verification fail, it also caused other errors related to my Google Place API

Vào Th 2, 20 thg 5, 2019 vào lúc 09:10 Robert Brunhage <robertb...@gmail.com> đã viết:
--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.

Doug Stevenson

unread,
May 20, 2019, 11:49:17 AM5/20/19
to fireba...@googlegroups.com
Hoang, it looks like you probably replied to the wrong thread.  If you're having problems with the Firebase console, please reach out to Firebase support.

You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/y3KFIELD4ag/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages