Python decimal Module
Example
Accurate decimal arithmetic:
from decimal import Decimal
print(Decimal('0.1') + Decimal('0.2'))
print(Decimal('1') / Decimal('4'))
Try it Yourself »
Definition and Usage
The decimal module provides support for fast correctly-rounded decimal floating point arithmetic.
Members
| Member | Description |
|---|---|
| BasicContext | Predefined context with basic precision. |
| Clamped | Signal raised when exponent is clamped to fit bounds. |
| Context | Arithmetic context with precision and rounding settings. |
| Decimal | Decimal floating point number class. |
| DivisionByZero | Signal raised on division by zero. |
| ExtendedContext | Predefined context with extended precision. |
| getcontext() | Get the current arithmetic context. |
| Inexact | Signal raised when rounding changes a result. |
| InvalidOperation | Signal raised on invalid operation. |
| localcontext() | Context manager for temporarily changing the context. |
| Overflow | Signal raised when result is too large to be represented. |
| ROUND_05UP | Round away from 0 if last digit is 0 or 5; otherwise to nearest. |
| ROUND_CEILING | Round toward +Infinity. |
| ROUND_DOWN | Round toward 0 (truncate). |
| ROUND_HALF_DOWN | Round to nearest with ties going toward 0. |
| ROUND_HALF_EVEN | Round to nearest with ties to even (banker's rounding). |
| ROUND_HALF_UP | Round to nearest with ties going away from 0. |
| ROUND_UP | Round away from 0. |
| Rounded | Signal raised when a result is rounded. |
| setcontext() | Set the current arithmetic context. |
| Underflow | Signal raised when result is too small to be represented. |