How to Round a Number in Python (round() Function & Decimal)
Learn how to round floating-point numbers in Python. Understand Banker's rounding, custom decimal precision, and formatting floats for display.
Explanation
Floating-point arithmetic in computers can sometimes lead to unexpected precision issues due to how binary systems store decimals (e.g., `0.1 + 0.2` evaluating to `0.30000000000000004`). When rendering numbers to users or preparing metrics, rounding numbers is a common requirement. Python provides several ways to achieve this.
The primary tool is the built-in `round(number, ndigits)` function. It takes a number and an optional precision count. If `ndigits` is omitted, it rounds to the nearest integer. If `ndigits` is provided, it rounds to that number of decimal places.
A key property of Python's `round()` function is that it uses "Banker's rounding" (round half to even). When a number is exactly halfway between two integers (e.g., `2.5` or `3.5`), Python rounds to the nearest *even* number. This means `round(2.5)` is `2`, and `round(3.5)` is `4`. While mathematically fair over large datasets to prevent upward bias, for precise financial calculations you should use the `decimal` module's quantization tools.
Step-by-Step Implementation
- 1
Call the built-in round(number) function to round a float to the nearest whole integer.
- 2
Pass a second argument (e.g., round(num, 2)) to specify the number of decimal places to keep.
- 3
Use the decimal module with ROUND_HALF_UP for strict financial rounding calculations.
Code Example
This script demonstrates rounding floats using the built-in round function, Banker's rounding examples, and the decimal module.
from decimal import Decimal, ROUND_HALF_UP
val = 3.14159
# 1. Round to nearest integer
print("Nearest Integer:", round(val))
# 2. Round to 2 decimal places
print("Two decimals:", round(val, 2))
# 3. Banker's Rounding demonstration (rounds to nearest even)
print("round(2.5):", round(2.5)) # Nearest even is 2
print("round(3.5):", round(3.5)) # Nearest even is 4
# 4. Math rounding up (Traditional rounding) using decimal module
amount = Decimal("2.5")
rounded_up = amount.quantize(Decimal("1"), rounding=ROUND_HALF_UP)
print("Decimal ROUND_HALF_UP (2.5):", rounded_up)Nearest Integer: 3
Two decimals: 3.14
round(2.5): 2
round(3.5): 4
Decimal ROUND_HALF_UP (2.5): 3Frequently Asked Questions
Why does round(2.5) return 2 instead of 3 in Python?
Python uses Banker's rounding, which rounds half numbers to the nearest even integer. This reduces statistical bias (rounding up half numbers always causes a slight upward drift over large datasets).
How do I round a number always up or always down?
Import the math module and use math.ceil(x) to round up to the next integer, or math.floor(x) to round down to the previous integer.
Related How-To Guides
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
Python Dictionary Methods
Learn Python dictionary methods. Complete reference guide for key-value pair insertions, retrievals, updates, and checks.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.