Precision Handling in Python
Last Updated :
19 Dec, 2025
Precision handling means controlling how many decimal places a number should have or how it should be rounded. It helps when you want a cleaner output, consistent formatting, or accurate calculations especially in tasks like finance, measurements, and scientific computations.
Given a number, the task is to control its precision either by rounding it or formatting it to a specific number of decimal places. For Example:
Input: x = 2.4
Output: Integral value = 2
Smallest integer greater than x = 3
Greatest integer smaller than x = 2
Let's explore different ways to do this task in Python.
Using round() and String Formatting
This method lets you reduce a floating-point number to a chosen number of decimal places. It is commonly used when you need a simple rounded value or want to display a number with fixed decimal digits.
Python
x = 8.88888
a = round(x, 2)
b = "{:.3f}".format(9.999)
print(a)
print(b)
Explanation:
- round(x, 2) reduces the number to 2 decimal places.
- "{:.3f}".format() formats a number so it always shows exactly 3 decimal places.
Using getcontext()
This method helps when calculations must follow a specific precision throughout the entire operation. By setting getcontext().prec, every Decimal calculation automatically respects that precision limit.
Python
from decimal import Decimal, getcontext
getcontext().prec = 3
r = Decimal('3') / Decimal('9')
print(r)
Explanation:
- getcontext().prec = 3 sets the total number of significant digits allowed.
- Decimal() ensures accurate decimal arithmetic without floating-point inaccuracies.
Using Decimal.quantize()
This method is used when you need a number to match an exact decimal structure. It allows you to enforce a fixed number of decimal digits, making it reliable for values like currency and measurements.
Python
from decimal import Decimal
x = Decimal('3.4536')
r = x.quantize(Decimal('0.00'))
print(r)
Explanation: quantize(Decimal('0.00')) formats the number to exactly 2 decimal places.
Using the Math Module
This method lets you control precision at the integer level. It helps when you want to remove decimal places or determine the nearest higher or lower integer.
Python
import math
x = 3.4536
print(math.trunc(x))
print(math.ceil(x))
print(math.floor(x))
Explanation:
- math.trunc(x) removes the decimal portion.
- math.ceil(x) gives the next higher whole number.
- math.floor(x) gives the next lower whole number.
This method formats numbers for display by giving you direct control over how many decimal places appear in the output.
Python
x = 3.4536
print('%.2f' % x)
print("{:.3f}".format(x))
print(round(x, 2))
print(f"{x:.2f}")
Output3.45
3.454
3.45
3.45
Explanation:
- '%.2f' % x displays the number with exactly 2 decimal places.
- "{:.3f}".format(x) displays 3 decimal places.
- f"{x:.2f}" uses modern string formatting for fixed decimal output.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice