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 |
|
<place> |
integer |
|
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.$round
返回的数据类型与输入表达式或值的数据类型匹配。
If rounding to a whole integer value, 如果舍入为整数值,$round
returns the value as an integer.$round
将以整数形式返回该值。
null
NaN
+/- Infinity
¶null
or refers to a field that is missing, $round
returns null
.null
值或引用缺少的字段,$round
返回null
。NaN
, $round
returns NaN
.NaN
,$round
返回NaN
。$round
returns negative or positive infinity respectively.$round
分别返回负无穷大或正无穷大。{ $round: [ NaN, 1] } |
NaN |
{ $round: [ null, 1] } |
null |
{ $round : [ Infinity, 1 ] } |
Infinity |
{ $round : [ -Infinity, 1 ] } |
-Infinity |
A collection named 名为samples
contains the following documents:samples
的集合包含以下文档:
value
rounded to the first decimal place:value
:
The operation returns the following results:操作返回以下结果:
value
rounded using the first digit to the left of the decimal:value
:
The operation returns the following results:操作返回以下结果:
value
rounded to the whole integer:value
:
The operation returns the following results:操作返回以下结果: