Mongodb multiplication multiplication operator
1 answer
Well, then I was going to propose to multiply and use $trunc
, but you can basically get away with just a chain $multiply
and $divide
here:
db.a.aggregate(
[
{ "$project": {
"total": {
"$divide": [
{ "$multiply": [
{ "$multiply": [ 4533, 0.0001 ] },
10000
]},
10000
]
}
}}
]
)
or simply $divide
, if this is what you mean:
db.a.aggregate([
{ "$project": { "total": { "$divide": [ 4533, 10000 ] } } }
])
What will both give you:
{ "total" : 0.4533 }
As noted, there is nothing new about math and floating point rounding, and as a general guideline, you should avoid using and just apply the "non-floating point" version of what you actually mean.
A long read, if you have time, is on What Every Computer Scientist Should Know About Floating Point Arithmetic .
0
source to share