$round (aggregation)

On this page本页内容

Definition定义

$round

New in version 4.2..

$round rounds a number to a whole integer or to a specified decimal place.将数字舍入为整数或指定的小数点。

$round has the following syntax:语法如下所示:

{ $round : [ <number>, <place> ] }
Field字段Type类型Description描述
<number> number

Can be any valid expression that resolves to a number. 可以是解析为数字的任何有效表达式Specifically, the expression must resolve to an integer, double, decimal, or long.具体来说,表达式必须解析为整数、双精度、十进制长整型

$round returns an error if the expression resolves to a non-numeric data type.如果表达式解析为非数字数据类型,$round将返回错误。

<place> integer

Optional. 可选Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g. -20 < place < 100. 可以是解析为-20到100(排除)之间的整数的任何有效表达式,亦-20 < place < 100Defaults to 0 if unspecified.如果未指定,则默认为0

  • If <place> resolves to a positive integer, $round rounds to <place> decimal places.如果<place>解析为正整数,$round舍入为<place>小数位。

    For example, $round : [1234.5678, 2] rounds to two decimal places and returns 1234.57.例如,$round : [1234.5678, 2]舍入到小数点后两位,返回1234.57

  • If <place> resolves to a negative integer, $round rounds using the digit <place> to the left of the decimal.如果<place>解析为负整数,则使用小数点左侧的数字<place>进行舍入。

    For example, $round : [1234.5678, -2] uses the 2nd digit to the left of the decimal (3) and returns 1200.例如,$round : [1234.5678, -2]使用小数点左边的第二位数字(3)并返回1200

    If the absolute value of <place> equals or exceeds the number of digits to the left of the decimal, $round returns 0.如果<place>的绝对值等于或超过小数点左边的位数,$round返回0

    For example, $round : [ 1234.5678, -4] specifies the fourth digit to the left of the decimal. 例如,$round : [ 1234.5678, -4]指定小数点左边的第四位数字。This equals the number of digits left of the decimal and returns 0.这等于小数点左边的位数,返回0

  • If <place> resolves to 0, $round rounds using the first digit to the right of the decimal and returns rounded integer value.如果<place>解析为0$round使用小数点右边的第一个数字进行舍入,并返回舍入整数值。

    For example, $round : [1234.5678, 0] returns 1234.例如,$round : [1234.5678, 0]返回1234

Behavior行为

Rounding Numbers Ending in 5以5结尾的四舍五入数字

To minimize the skew errors that are caused by always rounding upwards, numbers ending in 5 are rounded to the nearest even value. 为了最大限度地减少总是向上舍入导致的倾斜误差,以5结尾的数字被舍入到最接近的偶数值。This is the IEEE standard for floating point numbers and also works well operations across sequences.这是IEEE浮点数标准,也适用于跨序列的操作。

For example, consider this chart:例如,考虑这张图表:

OriginalRounded 1Rounded 0Rounded -1
124.5 124.5 124 120
125.5 125.5 126 130
25 25 25 20
12.5 12.5 12 10
2.25 2.2 2 0
2.45 2.5 2 0

The chart highlights a few points.图表突出了几点。

  • The $round function is not limited to floats. $round函数不限于浮动。(25 becomes 20).
  • Rounded numbers can still end in 5 四舍五入的数字仍然可以以5结尾(2.45 becomes 2.5)
  • The rounded value is determined by more than one digit舍入值由多个数字决定

For further discussion of the ‘Round Half to Even’ technique, see this article.有关“从一半到一半”技术的进一步讨论,请参阅本文

Returned Data Type返回的数据类型

If rounding to a specific decimal place, the data type returned by $round matches the data type of the input expression or value.如果四舍五入到特定的小数位,则$round返回的数据类型与输入表达式或值的数据类型匹配。

If rounding to a whole integer value, $round returns the value as an integer.如果舍入为整数值,$round将以整数形式返回该值。

null, NaN, and +/- Infinity

  • If the first argument resolves to a value of null or refers to a field that is missing, $round returns null.如果第一个参数解析为null值或引用缺少的字段,$round返回null
  • If the first argument resolves to NaN, $round returns NaN.如果第一个参数解析为NaN$round返回NaN
  • If the first argument resolves to negative or positive infinity, $round returns negative or positive infinity respectively.如果第一个参数解析为负无穷大或正无穷大,$round分别返回负无穷大或正无穷大。
Example示例Results结果
{ $round: [ NaN, 1] } NaN
{ $round: [ null, 1] } null
{ $round : [ Infinity, 1 ] } Infinity
{ $round : [ -Infinity, 1 ] } -Infinity

Example示例

A collection named samples contains the following documents:名为samples的集合包含以下文档:

{ _id: 1, value: 19.25 }
{ _id: 2, value: 28.73 }
{ _id: 3, value: 34.32 }
{ _id: 4, value: -45.39 }