定義
$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]小数点以下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]は小数点の左側にある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]小数点の左側の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 標準を使用しており、動作はその標準にコンシステントです。
高精度を必要とするアプリケーションで浮動点数が必要な場合は、Decimal128 値を検討してください。詳しくは、 BSON.Decimal を参照してください。128
通貨価値を保存する必要がある場合は、最小の通貨単位を使用する整数を検討してください。例、浮動点の代わりに、セントまたはペナルティを含む整数を使用します。
返されるデータ型
返されるデータ型は、入力式または値のデータ型と一致します。
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という名前のコレクションを作成します。
db.samples.insertMany( [ { _id: 1, value: 19.25 }, { _id: 2, value: 28.73 }, { _id: 3, value: 34.32 }, { _id: 4, value: -45.39 } ] )
次の集計は、
valueを小数第1位で丸めたものを返します。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 }