As I know, JavaScript uses IEEE 754 Floating point to treat real numbers. The problem arises when using it, it's quite common issue:
0.1 + 0.2 = 0.30000000000000004
Most of time, using the module like 'big.js', but the problem is when using MongoDB.
Look at this document:
{ "_id" : ObjectId("58ff8f721bbfbcd140e472b3"), "value" : 0.1 }
When I run $inc like this:
db.dec.update({ _id: ObjectId('58ff8f721bbfbcd140e472b3') }, { $inc: { value: 0.2 }});
The result is not good at all:
{ "_id" : ObjectId("58ff8f721bbfbcd140e472b3"), "value" : 0.30000000000000004 }
I know why it's happening, but is there a way to avoid this problem?
var nr = .1 + .2; console.log(nr, nr.toLocaleString("en", {style: "currency", currency: "USD"}))Number#toLocaleString() - Thomas(.1 + .2) === .3- Thomas