On this page本页内容
$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:语法如下所示:
<number> |
number | Can be any valid expression that resolves to a number. Specifically, the expression must resolve to an integer, double,
|
<place> |
integer | Optional Can be any valid expression that resolves to an integer between -20 and 100, exclusive. e.g.
|
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:例如,考虑这张图表:
Original | Rounded 1 | Rounded 0 | Rounded -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.图表突出了几点。
$round
function is not limited to floats. $round
函数不限于浮动。(25
becomes 20
).2.45
becomes 2.5
)For further discussion of the ‘Round Half to Even’ technique, see this article.有关“从一半到一半”技术的进一步讨论,请参阅本文。
If rounding to a specific decimal place, the data type returned by $round
matches the data type of the input expression or value.
If rounding to a whole integer value, $round
returns the value as an integer.
null
, NaN
, and +/- Infinity
¶null
or refers to a field that is missing, $round
returns null
.NaN
, $round
returns NaN
.$round
returns negative or positive infinity respectively.{ $round: [ NaN, 1] } |
NaN |
{ $round: [ null, 1] } |
null |
{ $round : [ Infinity, 1 ] } |
Infinity |
{ $round : [ -Infinity, 1 ] } |
-Infinity |
A collection named samples
contains the following documents:
value
rounded to the first decimal place:
The operation returns the following results:操作返回以下结果:
value
rounded using the first digit to the left of the decimal:
The operation returns the following results:操作返回以下结果:
value
rounded to the whole integer:
The operation returns the following results:操作返回以下结果: