Okay then I was going to suggest multiplying and using $trunc
, but you can basically get away with just chaining $multiply
and $divide
here:
db.a.aggregate(
[
{ "$project": {
"total": {
"$divide": [
{ "$multiply": [
{ "$multiply": [ 4533, 0.0001 ] },
10000
]},
10000
]
}
}}
]
)
or just $divide
when that is what you mean:
db.a.aggregate([
{ "$project": { "total": { "$divide": [ 4533, 10000 ] } } }
])
Which both will give you:
{ "total" : 0.4533 }
As noted, There is nothing new about floating point math and rounding, and as a general course you should avoid usage and simply apply the "non-floating point" version of what you actually mean.
The long read if you have a while is at What Every Computer Scientist Should Know About Floating-Point Arithmetic.