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: ...

Get Python: Essential Reference, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.