|
| 1 | +========== |
| 2 | +Decimal128 |
| 3 | +========== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +MongoDB 3.4 introduced support for a :manual:`Decimal128 BSON type |
| 17 | +</manual/release-notes/3.4/#decimal-type>`, which is a 128-bit decimal-based |
| 18 | +floating-point value capable of emulating decimal rounding with exact precision. |
| 19 | +This functionality is intended for applications that handle :manual:`monetary |
| 20 | +data </tutorial/model-monetary-data>`, such as financial and tax computations. |
| 21 | + |
| 22 | +The :php:`MongoDB\BSON\Decimal128 <mongodb-bson-decimal128>` class, which was |
| 23 | +introduced in :php:`PHP driver <mongodb>` 1.2.0, may be used to work with this |
| 24 | +type in PHP. |
| 25 | + |
| 26 | +Working with Decimal128 Values |
| 27 | +------------------------------ |
| 28 | + |
| 29 | +Inserting a Decimal128 |
| 30 | +~~~~~~~~~~~~~~~~~~~~~~ |
| 31 | + |
| 32 | +The following example inserts a value of type ``Decimal128`` into the ``price`` |
| 33 | +field of a collection named ``inventory``: |
| 34 | + |
| 35 | +.. code-block:: php |
| 36 | + |
| 37 | + <?php |
| 38 | + |
| 39 | + $collection = (new MongoDB\Client)->demo->inventory; |
| 40 | + |
| 41 | + $collection->insertOne([ |
| 42 | + '_id' => 1, |
| 43 | + 'item' => '26-inch monitor', |
| 44 | + 'price' => new MongoDB\BSON\Decimal128('428.79'), |
| 45 | + ]); |
| 46 | + |
| 47 | + $item = $collection->findOne(['_id' => 1]); |
| 48 | + |
| 49 | + var_dump($item); |
| 50 | + |
| 51 | +The output would then resemble:: |
| 52 | + |
| 53 | + object(MongoDB\Model\BSONDocument)#9 (1) { |
| 54 | + ["storage":"ArrayObject":private]=> |
| 55 | + array(3) { |
| 56 | + ["_id"]=> |
| 57 | + int(1) |
| 58 | + ["item"]=> |
| 59 | + string(15) "26-inch monitor" |
| 60 | + ["price"]=> |
| 61 | + object(MongoDB\BSON\Decimal128)#13 (1) { |
| 62 | + ["dec"]=> |
| 63 | + string(6) "428.79" |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +Mathematical Operations with BCMath |
| 69 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +The :php:`PHP driver <mongodb>` does not provide any functionality for working |
| 72 | +with ``Decimal128`` values; however, the string representation of a |
| 73 | +:php:`MongoDB\BSON\Decimal128 <mongodb-bson-decimal128>` object may be used |
| 74 | +with PHP's :php:`BCMath <bcmath>` extension. |
| 75 | + |
| 76 | +The following example adds two ``Decimal128`` values and creates a new |
| 77 | +``Decimal128`` value with the result from :php:`bcadd() <bcadd>`: |
| 78 | + |
| 79 | +.. code-block:: php |
| 80 | + |
| 81 | + <?php |
| 82 | + |
| 83 | + $lhs = new MongoDB\BSON\Decimal128('1.234'); |
| 84 | + $rhs = new MongoDB\BSON\Decimal128('5.678'); |
| 85 | + $sum = new MongoDB\BSON\Decimal128(bcadd($lhs, $rhs)); |
| 86 | + |
| 87 | + var_dump($sum); |
| 88 | + |
| 89 | +The output would then resemble:: |
| 90 | + |
| 91 | + object(MongoDB\BSON\Decimal128)#4 (1) { |
| 92 | + ["dec"]=> |
| 93 | + string(1) "6" |
| 94 | + } |
| 95 | + |
| 96 | +This does not match the expected result of "6.912". Each operation in the BCMath |
| 97 | +API uses a scale to determine the number of decimal digits in the result. The |
| 98 | +default scale is zero, which is why the above example produces a result with no |
| 99 | +decimal precision. |
| 100 | + |
| 101 | +In the following example, we use a scale of three for :php:`bcadd() <bcadd>` to |
| 102 | +obtain the expected result: |
| 103 | + |
| 104 | +.. code-block:: php |
| 105 | + |
| 106 | + <?php |
| 107 | + |
| 108 | + $lhs = new MongoDB\BSON\Decimal128('1.234'); |
| 109 | + $rhs = new MongoDB\BSON\Decimal128('5.678'); |
| 110 | + $sum = new MongoDB\BSON\Decimal128(bcadd($lhs, $rhs, 3)); |
| 111 | + |
| 112 | + var_dump($sum); |
| 113 | + |
| 114 | +The output would then resemble:: |
| 115 | + |
| 116 | + object(MongoDB\BSON\Decimal128)#4 (1) { |
| 117 | + ["dec"]=> |
| 118 | + string(5) "6.912" |
| 119 | + } |
| 120 | + |
| 121 | +In lieu of specifying a scale for each operation, a default scale may be set via |
| 122 | +:php:`bcscale() <bcscale>` or the :php:`bcmath.scale INI setting |
| 123 | +<manual/en/bc.configuration.php#ini.bcmath.scale>`. The ``Decimal128`` type |
| 124 | +supports up to 34 decimal digits (i.e. significant digits). |
0 commit comments