정의
$round$roundrounds a number to a whole integer or to a specified decimal place.$round의 구문은 다음과 같습니다:{ $round : [ <number>, <place> ] } 필드유형설명<number>숫자
<place>integer
선택 사항 -20에서 100 사이의 정수로 확인되는 유효한 표현식 일 수 있습니다(단, 제외). 예:
-20 < place < 100. 지정하지 않으면 기본값은0입니다.If
<place>resolves to a positive integer,$roundrounds to<place>decimal places.예를 들어
$round : [1234.5678, 2]은 소수점 둘째 자리로 반올림하여1234.57을 반환합니다.If
<place>resolves to a negative integer,$roundrounds using the digit<place>to the left of the decimal.예를 들어
$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,$roundreturns0.예를 들어,
$round : [ 1234.5678, -4]소수점 왼쪽의 네 번째 숫자를 지정합니다. 이는 소수점에서 남은 자릿수와 같으며0을 반환합니다.If
<place>resolves to0,$roundrounds using the first digit to the right of the decimal and returns rounded integer value.예를 들어
$round : [1234.5678, 0]은1235을 반환합니다.
행동
짝수 값으로 반올림
When rounding on a value of 5, $round rounds to the nearest even value. For example, consider the following sample documents:
{_id : 1, "value" : 10.5}, {_id : 2, "value" : 11.5}, {_id : 3, "value" : 12.5}, {_id : 4, "value" : 13.5}
$round : [ "$value", 0] 다음을 반환합니다.
{_id : 1, "value" : 10}, {_id : 2, "value" : 12}, {_id : 3, "value" : 12}, {_id : 4, "value" : 14}
10.5 값은 짝수 값 10 에 가장 가깝고 값 11.5 및 12.5 값은 짝수 값 12 에 가장 가깝습니다. 가장 가까운 짝수 값으로 반올림하면 항상 상향 또는 하향 반올림보다 반올림된 데이터가 더 고르게 분포됩니다.
부동 소수점 숫자를 사용한 반올림 정밀도
소수점 구분 기호 뒤에 20 자리가 포함된 배정밀도 부동 점 숫자 0.05 를 인쇄하면 0.05000000000000000278가 표시되며, 이는 0.05 보다 약간 크고 0.1로 반올림됩니다.
MongoDB 부동 점 계산에 IEEE 754 표준을 사용하며 동작은 해당 표준과 일관적인 합니다.
If you need a floating point number for an application that requires high precison, consider a Decimal128 value. For details, see BSON.Decimal128.
통화 값을 저장 해야 하는 경우 가장 낮은 통화 표시 단위를 사용하는 정수를 고려하세요. 예시 들어, 부동 점 숫자 대신 센트 또는 페니가 포함된 정수를 사용합니다.
반환된 데이터 유형
반환된 데이터 유형 은 입력 표현식 또는 값의 데이터 유형 유형과 일치합니다.
null, NaN 및 +/- Infinity
If the first argument resolves to a value of
nullor refers to a field that is missing,$roundreturnsnull.If the first argument resolves to
NaN,$roundreturnsNaN.If the first argument resolves to negative or positive infinity,
$roundreturns negative or positive infinity respectively.
예시 | 결과 |
|---|---|
|
|
|
|
|
|
|
|
예시
다음 문서를 사용하여 samples 라는 이름의 샘플 collection을 생성합니다.
db.samples.insertMany( [ { _id: 1, value: 19.25 }, { _id: 2, value: 28.73 }, { _id: 3, value: 34.32 }, { _id: 4, value: -45.39 } ] )
다음 집계는 소수점 첫째 자리로 반올림된
value를 반환합니다.db.samples.aggregate([ { $project: { roundedValue: { $round: [ "$value", 1 ] } } } ]) 이 연산은 다음과 같은 결과를 반환합니다.
{ "_id" : 1, "roundedValue" : 19.2 } { "_id" : 2, "roundedValue" : 28.7 } { "_id" : 3, "roundedValue" : 34.3 } { "_id" : 4, "roundedValue" : -45.4 } 다음 집계는 소수점 왼쪽 첫 번째 숫자를 사용하여 반올림된
value를 반환합니다.db.samples.aggregate([ { $project: { roundedValue: { $round: [ "$value", -1 ] } } } ]) 이 연산은 다음과 같은 결과를 반환합니다.
{ "_id" : 1, "roundedValue" : 10 } { "_id" : 2, "roundedValue" : 20 } { "_id" : 3, "roundedValue" : 30 } { "_id" : 4, "roundedValue" : -50 } 다음 집계는 정수로 반올림된
value반환합니다.db.samples.aggregate([ { $project: { roundedValue: { $round: [ "$value", 0 ] } } } ]) 이 연산은 다음과 같은 결과를 반환합니다.
{ "_id" : 1, "roundedValue" : 19 } { "_id" : 2, "roundedValue" : 29 } { "_id" : 3, "roundedValue" : 34 } { "_id" : 4, "roundedValue" : -45 }