decimal
On most systems, the Python float data type is represented using a binary floating-point encoding such as the IEEE 754 standard. A subtle consequence of the binary encoding is that decimal values such as 0.1 can’t be represented exactly. Instead, the value is 0.10000000000000001. This inexactness carries over to calculations involving floating-point numbers and can sometimes lead to unexpected results (for example, 3*0.1 == 0.3 evaluates as False).
The decimal module provides generalized support for decimal floating-point numbers. These numbers allow for exact representation of decimals. In addition, parameters such as precision, significant digits, and rounding behavior can be controlled.
The decimal module defines two basic data types: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access