0

I am calling update from node to add money to a user's account. Starting from 0, these two sequential updates: 83.94 and then 546.13 result in 630.0699999999999.

This is the same result as if you do this in JS

83.94+566.13

This is the mongo call:

xxx.update({_id: xId},
    {
        $inc: {money: dollars}
    });

Is there any way to prevent this and get reasonable behavior? (besides converting to pennies instead of dollars).

Do I really have to "round to 2" every time I read this field?

Eric Oemig
  • 487
  • 6
  • 9
  • 2
    Maybe interesting? [MongoDB - What about Decimal type of value?](https://stackoverflow.com/questions/11541939/mongodb-what-about-decimal-type-of-value#11542549). Welcome to the issues with floating point numbers. ;-/ Also worth a look - maybe check this first?: [Model Monetary Data](https://docs.mongodb.com/manual/tutorial/model-monetary-data/) – Ryan Vincent Oct 31 '18 at 21:34