js的精度,mongodb的精度,怎么破啊
发布于 5 年前 作者 yhj8899 5073 次浏览 来自 问答

js:6 + 0.55 = 6.549999999999999 mongodb: 6 $inc 0.55 ,也会变成 6.549999999999999 怎么办啊? 会导致金额乱糟糟的。

8 回复
$ mongo test
MongoDB shell version: 3.2.10
connecting to: test
>
> db.test.insertOne({ x: 0.1 })
{
    "acknowledged" : true,
    "insertedId" : ObjectId("588a3f711554cc7d70642fa1")
}
> db.test.updateOne({}, { $inc: { x: 0.2 } })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.test.findOne()
{ "_id" : ObjectId("588a3f711554cc7d70642fa1"), "x" : 0.30000000000000004 }
>

$ node
> 0.1
0.1
> 0.2
0.2
> 0.1 * 0.2
0.020000000000000004
> 0.1 * 0.1
0.010000000000000002
>


@waitingsong 你们是怎么解决呢?

@yhj8899 我这边数据库都是存的整数,用的时候除以100

回到顶部