$round (Round)
Last updated: 2026-10-09
$round (Round)
$round rounds a number, optionally to a specified decimal place. It takes the value and an optional place argument: 0 or omitted rounds to an integer, positive places keep decimals, and negative places round to tens, hundreds, and so on. It is used to normalize amounts, scores, or other displayed values. A trailing .5 rounds away from zero (2.5 becomes 3, -2.5 becomes -3); the difference from $trunc shows up only on negatives and the .5 boundary. Any null or missing argument yields null.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $round: [ <value>, <place> ] }
⚙️ Parameters
| numeric value | A numeric value or an expression resolving to one. |
|---|---|
| position | Optional decimal places; 0 or omitted rounds to an integer, negative values round to tens or higher. |
Returns:The rounded value; null when null or missing
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $round: keep two decimal places for amounts
db.orders.aggregate([
{ $project: {
rounded: { $round: ['$price', 2] }
} }
])
Output:
{ '_id': 1, 'rounded': 19.99 }
{ '_id': 2, 'rounded': 12.35 }
📌 Notes
- .5 rounds away from zero, so -2.5 becomes -3
- A negative place rounds to tens or hundreds, e.g. $round: [123, -1] gives 120
- Compared with $trunc, $round carries on negative fractions while $trunc simply cuts them
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $round and $trunc?
A$round rounds with .5 going away from zero while $trunc cuts toward zero; the difference shows on negatives.
QHow do I round to the nearest ten with $round?
APass -1 as the place, so $round: [123, -1] gives 120.
QWhat does $round return for -2.5?
AIt returns -3, since .5 rounds away from zero.