Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

When should you use Decimal instead of float in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 26, 2026
5 min read

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.

Table of Contents

  • Float versus Decimal at a glance
  • When Decimal is the right choice
  • Creating Decimal values correctly
  • Precision, rounding and quantize
  • Mixing Decimal with other types
  • Performance and the C implementation
  • Frequently Asked Questions
    • Is Decimal always more accurate than float?
    • How do I store Decimal values in a database?
    • Should I use Decimal for JSON?
    • Which rounding mode should I choose?
  • Summary

Float versus Decimal at a glance

FeaturefloatDecimal
StorageBinary (IEEE 754 double)Decimal digits
0.1 + 0.2 == 0.3FalseTrue
Default precisionAbout 15 to 17 significant digits28 significant digits (configurable)
SpeedVery fast, hardware supportedSlower (software, C implementation)
Rounding controlLittleExplicit modes and contexts
Best forScience, graphics, machine learningMoney, billing, exact decimal rules
python
text

When Decimal is the right choice

Choose Decimal when:

  • The values are currency or invoices and totals must match to the cent.
  • A rule or regulation defines how to round, for example "round half up to two places".
  • You must show or store exact decimal digits, such as 19.90.
  • You compare computed results for equality and need that to be reliable.

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.

Creating Decimal values correctly

Always build Decimal from a string or an integer, not from a float.

python
text

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").

Precision, rounding and quantize

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

python
text

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.

Mixing Decimal with other types

Decimal works with integers, but not with floats in arithmetic:

python
text

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.

Performance and the C implementation

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.

Frequently Asked Questions

Is Decimal always more accurate than float?

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.

How do I store Decimal values in a database?

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.

Should I use Decimal for JSON?

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.

Which rounding mode should I choose?

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.

Summary

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.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?