Reputation: 497
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?
Upvotes: 0
Views: 893
Reputation: 497
Thanks @ryan. Just what I needed. https://docs.mongodb.com/manual/tutorial/model-monetary-data/ https://docs.mongodb.com/manual/core/shell-types/#shell-type-decimal
Upvotes: 1