Eric Oemig
Eric Oemig

Reputation: 497

Prevent rounding errors when using $inc in mongodb on US currency?

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

Answers (1)

Related Questions