The $round operator rounds numeric values to a chosen number of decimal places inside MongoDB aggregation pipelines. Use it to clean up sensor readings, format prices, or simplify report output without changing stored data.
01
Round Numbers
Control precision.
02
place Arg
Decimals or tens.
03
Array Syntax
[ number, place ].
04
$project
Add rounded fields.
05
Use Cases
Finance, stats, UI.
06
Null Safety
Know null behavior.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $round operator rounds a numeric expression to a specified number of decimal places. For example, rounding 21.578 to two places gives 21.58, and rounding 1234 to the nearest hundred (place: -2) gives 1200.
This is especially useful when presenting data, computing financial totals, or normalizing sensor readings where full floating-point precision is unnecessary or confusing for end users.
💡
Beginner Tip
Think of $round like JavaScript’s rounding helpers, but with an explicit place argument. Positive place counts digits after the decimal; negative place rounds left of the decimal (tens, hundreds, etc.).
Foundation
📝 Syntax
The $round operator accepts an array of two expressions:
Multiply first, then round. This pattern avoids storing long floating-point tails in invoice output.
Applications
🚀 Use Cases
Data presentation — show readable numbers in dashboards and API responses.
Financial calculations — round currency, tax, and totals to required precision.
Statistical summaries — format averages, rates, and percentages cleanly.
Reporting buckets — snap large values to tens or hundreds for summary charts.
🧠 How $round Works
1
MongoDB evaluates both operands
The pipeline resolves the number and the place value to numbers (or null).
Input
2
The value is rounded to the target precision
Positive place rounds decimal digits; zero rounds to an integer; negative rounds to tens, hundreds, and so on.
Round
3
The rounded number is returned
The result is a numeric value suitable for display, further math, or storage in a projected field.
Output
=
🔢
Precision-controlled number
Use in projections, grouped averages, or chained with $multiply and $divide.
Wrap Up
Conclusion
The $round operator rounds numeric values to a specified number of decimal places in MongoDB aggregation pipelines. It keeps reports readable and financial output precise without manual formatting in application code.
Remember: positive place for decimals, zero for integers, negative for tens and hundreds. Next in the series: $rtrim.
Round at the end of calculations (multiply/divide first, then round)
Use place: 2 for currency-style output
Use negative place for coarse summary reporting
Handle null inputs with $ifNull when fields may be missing
Keep raw values in storage; round in projections for display
❌ Don’t
Round too early in multi-step financial calculations (rounding errors add up)
Assume place truncates — it rounds, not chops digits
Use $round as a query filter (it is expression-only)
Forget that null input returns null
Confuse rounding with $floor or $ceil (always down/up)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $round
Use these points when rounding numbers in pipelines.
5
Core concepts
🔢01
Precision
Control decimals.
Purpose
📝02
[ n, place ]
Two operands.
Syntax
🛠03
place sign
+ / 0 / −.
place
🗃04
Expressions OK
Not just fields.
Input
⚠05
null
Null in → null out.
Edge case
❓ Frequently Asked Questions
$round rounds a numeric value to a specified number of decimal places. Positive place values round after the decimal point; zero rounds to a whole number; negative place values round to tens, hundreds, and so on.
The array syntax is { $round: [ <number>, <place> ] }. You can also use { $round: { input: <expression>, place: <expression> } }. Use inside $project, $addFields, or $set.
place is the number of decimal places. place: 2 rounds to two decimals (21.578 → 21.58). place: 0 rounds to an integer. place: -2 rounds to the nearest hundred (1234 → 1200).
Yes. The first argument can be a field path like "$price" or any numeric expression, such as { $divide: ["$total", "$count"] }.
If the number expression resolves to null, $round returns null. It does not throw an error.