0

I'm want to round off to 2 decimal places.

I'm using it in 2 places, one is in the aggregate query, using $round which I understood that it works in the following way: Ex: {$round : [46.305, 2]} => 46.3 So basically it is getting the 3rd digit after the decimal and only for digits 6 and greater, it is rounding off to next digit. That is, in 46.305, 3rd digit after the decimal is 5 which is less than 6. Hence rounding off to 46.3

In another query, I'm using Math.round(). Ex: Math.round(46.305 * 100)/100 => 46.31. Because it is considering from .5 and greater and rounding off to next digit.

So here I'm getting conflicts in the values

Aggregate is returning 46.3 and math.round() is giving 46.31

Ideally, I want to round off to 2 digits and want to have the same values in both cases.

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
Shambhavi_RB
  • 111
  • 1
  • 8
  • The [documentation](https://docs.mongodb.com/manual/reference/operator/aggregation/round/#rounding-to-even-values) says `$round` rounds to the nearest **even** value. Which makes me wonder how it output your result, so I tried several cases in this [playground](https://mongoplayground.net/p/hEkDCq-Tlxg) and I'm even more confused O_o. – thammada.ts Jun 29 '20 at 14:58
  • There is a discussion of this behavior at https://stackoverflow.com/questions/977796/why-does-math-round2-5-return-2-instead-of-3 – Joe Jun 29 '20 at 20:26

0 Answers0