Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Module

decimal

Python’s decimal module provides correctly rounded decimal arithmetic with configurable precision, rounding, exponent limits, and signal handling. It is designed for exact decimal-style and controlled rounded calculations.

How it works

The module represents decimal values independently of binary floating-point and performs arithmetic under a context. The context controls precision, rounding, exponent limits, and which signals become exceptions; localcontext() temporarily changes those settings for a block.

Syntax

from decimal import Decimal, getcontext, localcontext
Decimal(value)
getcontext()
localcontext(ctx=None)

Parameters

NameTypeDescription
valueint | str | tuple | floatA value accepted by Decimal, commonly an integer, string, tuple, or float.
cContextThe active arithmetic context used by context functions and operations.

Returns

Decimal operations return new Decimal objects; getcontext() returns the current Context and setcontext() changes the active Context.

Key points

  • decimal supplies correctly rounded decimal floating-point operations.
  • Precision can be configured through the active context rather than being fixed like typical binary hardware arithmetic.
  • Its model consists of Decimal values, an arithmetic context, and signals raised during operations.
  • Decimal instances cannot be changed in place; arithmetic returns new values.
  • A typical workflow imports decimal and inspects or adjusts the current context with getcontext().
  • The module supports exact unrounded decimal-style calculations as well as rounded floating-point calculations.

Common mistakes

  • Passing a float preserves its exact binary value, which may produce many unexpected decimal digits; use a string when you mean a human-written decimal.

Example: Controlled decimal arithmetic

from decimal import Decimal, getcontext, localcontext

getcontext().prec = 6
amount = Decimal("1.20")
rate = Decimal("0.075")

with localcontext() as ctx:
    ctx.prec = 10
    total = amount * (Decimal("1") + rate)

print(total)  # 1.2900

Construct Decimal values from strings, use the current context for normal arithmetic, and temporarily increase precision inside localcontext().

When to use it

Financial calculations

Money, invoices, tax, and prices where decimal rounding must be explicit.

High-precision arithmetic

Measurements or algorithms requiring more configurable precision than binary float provides.

Validated numeric workflows

Operations where rounding, overflow, invalid operations, or division by zero must be monitored.

Interview questions

QWhy use Decimal instead of float?

Decimal provides decimal floating-point arithmetic with user-adjustable precision, unlike binary float arithmetic. It is useful when decimal representation and rounding behavior matter, such as accounting calculations.

Follow-up: How should Decimal values be constructed from user input? Construct them from decimal strings or integers, avoiding float inputs when the intended value is a decimal quantity.

QWhat does the decimal context control?

A context is the arithmetic environment for Decimal operations. It controls precision, rounding, exponent limits, and which signals are trapped as exceptions.

Follow-up: How can one change settings without affecting surrounding code? Use localcontext() as a context manager, then modify the copied context inside the block.

QAre Decimal objects mutable, and how are trailing zeros handled?

Decimal objects are immutable, so operations create new Decimal results rather than modifying their operands. Their representation retains significant trailing zeros supplied during construction.

Follow-up: What does precision mean for an existing Decimal? Context precision limits arithmetic results, not the digits already stored in a Decimal object.

  • Decimal
  • Context
  • getcontext()
  • setcontext()
  • localcontext()
  • ROUND_HALF_EVEN
  • InvalidOperation
  • DivisionByZero

Source: decimal — Decimal fixed-point and floating-point arithmetic. Checked 2026-10-16.