AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$round(式演算子)

$round

$round rounds a number to a whole integer or to a specified decimal place.

$round の構文は次のとおりです。

{ $round : [ <number>, <place> ] }
フィールド
タイプ
説明

<number>

数値

数値に変換される有効なであればどれでもかまいません。具体的には、式は整数、double、 decimal 、またはlongに変換される必要があります。

$round 式が数値以外のデータ型に解決された場合はエラーを返します。

<place>

integer

任意 -20から100までの整数(排他的)に変換される有効な であればどれでもかまいません。例-20 < place < 100。指定しない場合、デフォルトは 0 です。

  • If <place> resolves to a positive integer, $round rounds to <place> decimal places.

    たとえば、 $round : [1234.5678, 2] 小数点以下2桁に丸められ、 1234.57 が返されます。

  • If <place> resolves to a negative integer, $round rounds 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, $round returns 0.

    たとえば、 $round : [ 1234.5678, -4] 小数点の左側の4番目の桁を指定します。これは小数の左の桁数に等しく、0 を返します。

  • If <place> resolves to 0, $round rounds 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.512.5 は偶数値 12に最も近い値です。最も近い偶数値に丸めると、四捨五入されたデータが、通常の切り上げまたは切り捨てよりも均等に分散されます。

小数点の後に 20 桁の倍精度浮動点 0.05 を出力すると、0.05000000000000000278 が表示されます。これは 0.05 よりわずかに大きく、0.1 に丸められます。

MongoDB は浮動点計算にIEEE 754 標準を使用しており、動作はその標準にコンシステントです。

高精度を必要とするアプリケーションで浮動点数が必要な場合は、Decimal128 値を検討してください。詳しくは、 BSON.Decimal を参照してください。128

通貨価値を保存する必要がある場合は、最小の通貨単位を使用する整数を検討してください。例、浮動点の代わりに、セントまたはペナルティを含む整数を使用します。

返されるデータ型は、入力式または値のデータ型と一致します。

  • If the first argument resolves to a value of null or refers to a field that is missing, $round returns null.

  • If the first argument resolves to NaN, $round returns NaN.

  • If the first argument resolves to negative or positive infinity, $round returns negative or positive infinity respectively.

結果

{ $round: [ NaN, 1] }

NaN

{ $round: [ null, 1] }

null

{ $round : [ Infinity, 1 ] }

Infinity

{ $round : [ -Infinity, 1 ] }

-Infinity

次のドキュメントを含む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 }
このページを評価

項目一覧