Use Decimal instead of float whenever the numbers are decimal quantities that must be exact, such as money, prices, tax rates or measurements with a fixed number of decimal places. A float stores a binary approximation, so 0.1 + 0.2 is not 0.3, while Python Decimal stores decimal digits and gives Decimal("0.1") + Decimal("0.2") == Decimal("0.3"). Keep float for scientific and numeric work where speed matters more than exact decimal results.
This article explains when each type is the right choice, how to create Decimal values correctly, how rounding and precision work, and which mistakes to avoid. It targets Python 3.13 and 3.14.
| Feature | float | Decimal |
|---|---|---|
| Storage | Binary (IEEE 754 double) | Decimal digits |
0.1 + 0.2 == 0.3 | False | True |
| Default precision | About 15 to 17 significant digits | 28 significant digits (configurable) |
| Speed | Very fast, hardware supported | Slower (software, C implementation) |
| Rounding control | Little | Explicit modes and contexts |
| Best for | Science, graphics, machine learning | Money, billing, exact decimal rules |
Choose Decimal when:
19.90.Stay with float when you run simulations, statistics, trigonometry or machine-learning code. Rounding errors of one part in 10^16 are irrelevant there, and float is much faster. Many projects also store money as integers of the smallest unit (cents) as a simple alternative, which avoids both types.
Always build Decimal from a string or an integer, not from a float.
Decimal(0.1) copies the exact value of the already-inexact float. If you must convert a float, first decide how many places are meaningful, for example Decimal(str(x)) or Decimal(f"{x:.2f}").
Decimal also keeps trailing zeros, which signals precision: Decimal("1.10") prints as 1.10, yet compares equal to Decimal("1.1").
The context controls precision, which counts significant digits (not decimal places), and rounding mode. The default precision is 28 and the default rounding is ROUND_HALF_EVEN (banker's rounding).
Use quantize to round to a fixed number of decimal places, for example Decimal("0.01") for cents. With the default ROUND_HALF_EVEN, Decimal("2.675") has a clear exact value, so it rounds to 2.68. Compare the last line: the float 2.675 is stored as slightly less than 2.675, so round gives 2.67. Prefer localcontext() over changing the global context so that threads and libraries are not affected.
Rounding only at the end of the calculation, rather than at every step, avoids drift in totals unless your business rule says otherwise.
Decimal works with integers, but not with floats in arithmetic:
This is deliberate: it stops silent mixing of exact and inexact values. Comparisons with floats are allowed, but results follow exact binary values, so Decimal("0.1") == 0.1 is False.
Since Python 3.3 the decimal module uses the C library libmpdec, which is far faster than the old pure-Python version. It is still slower than float, often by an order of magnitude for simple operations, so measure before using it in hot loops. For large numeric arrays use NumPy floats and apply decimal rounding only at the boundary where values become money.
Only for decimal fractions. Decimal(1) / Decimal(3) is still rounded to 28 digits. If you need exact ratios, fractions.Fraction is the exact choice.
Use a fixed-point column such as NUMERIC(12, 2). Database drivers such as psycopg return Decimal for it. Avoid FLOAT or REAL columns for money.
JSON has only one number type and most parsers read it as float. Send money as a string or as integer cents, and parse it into Decimal on your side.
ROUND_HALF_UP is what most people expect from school math, while ROUND_HALF_EVEN reduces bias in large sets. Follow the rule your business or regulation defines.
Use Decimal for money and exact decimal rules, build it from strings, round explicitly with quantize, and use localcontext to change precision safely. Keep float for scientific and performance-sensitive work, and use Fraction when you need exact ratios.