decimal128
– Support for BSON Decimal128
New in version 3.4.
Note
The Decimal128 BSON type requires MongoDB 3.4+.
class bson.decimal128.Decimal128
(value)
BSON Decimal128 type:
Note
>>> Decimal128(".13.1")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
>>>
>>> Decimal128("1E-6177")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.Inexact: [<class 'decimal.Inexact'>]
>>>
>>> Decimal128("1E6145")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
decimal.Overflow: [<class 'decimal.Overflow'>, <class 'decimal.Rounded'>]
To ensure the result of a calculation can always be stored as BSON Decimal128 use the context returned by create_decimal128_context():
To match the behavior of MongoDB’s Decimal128 implementation str(Decimal(value)) may not match str(Decimal128(value)) for NaN values:
>>> Decimal128(Decimal('NaN'))
Decimal128('NaN')
>>> Decimal128(Decimal('-NaN'))
Decimal128('NaN')
>>> Decimal128(Decimal('-sNaN'))
Decimal128('NaN')
However, will return the exact value:
Two instances of Decimal128 compare equal if their Binary Integer Decimal encodings are equal:
>>> Decimal128('NaN') == Decimal128('NaN')
True
>>> Decimal128('NaN').bid == Decimal128('NaN').bid
True
This differs from comparisons for NaN:
bid
classmethod
from_bid
(value)Create an instance of Decimal128 from Binary Integer Decimal string.
Parameters: to_decimal
()Returns an instance of for this Decimal128.
bson.decimal128.create_decimal128_context
()
Returns an instance of appropriate for working with IEEE-754 128-bit decimal floating point values.