$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.

CategoryMath
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $round: [ <value>, <place> ] }

⚙️ Parameters

numeric valueA numeric value or an expression resolving to one.
positionOptional 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏