Skip to main content

How to Round a Number in Python with round

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A price of 3.14159 reads better as 3.14. Python's built-in round does that: give it a number and how many decimal places to keep. It has one rule that surprises people, which this guide shows too, with real output from Python 3.15.2.

Syntax​

Syntax
round(<number>, <decimal places>)
round(<number>)
PartWhat it means
<number>The number to round
<decimal places>Optional. How many digits to keep after the decimal point. Without it, you get a whole number

The built-in functions reference: "Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input."

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

rounding.py
price = 3.14159

print(round(price, 2))
print(round(price))
print(round(2.5))
print(round(3.5))

The rounding.py script in a macOS-style code window: price = 3.14159, then print of round(price, 2), round(price), round(2.5) and round(3.5).

Step 2: Run It​

Terminal
uv run python rounding.py
Output
3.14
3
2
4

The script's output in a terminal: 3.14, 3, 2 and 4.

Two decimal places gave 3.14, and no decimal places gave 3. 2.5 became 2 but 3.5 became 4: a number exactly halfway goes to the even neighbor. The reference: "if two multiples are equally close, rounding is done toward the even choice".

Common Mistakes​

MistakeWhat happensDo this instead
Expecting halves to always round upround(2.5) gave 2That's the rule for exact halves; for money, see decimal below
Expecting round(2.675, 2) to give 2.68It gave 2.67: the float stored for 2.675 is 2.67499999999999982236Use the decimal module when exact decimal digits matter

Troubleshooting​

SymptomLikely causeFix
A rounded value looks one digit offBinary floats can't store most decimal fractions exactlyPrint it with f"{value:.20f}" to see the stored value

FAQ​

Does round give an int or a float?​

Without decimal places, an int; with them, a float: type(round(3.7)) was <class 'int'> and type(round(3.7, 1)) was <class 'float'>.

Can I round to tens or hundreds?​

Yes, with a negative number of places: round(1234, -2) gave 1200.

Conclusion​

round(price, 2) gave 3.14 and round(price) gave 3. Halfway values go to the even neighbor, and floats can make a value sit just below the halfway point.

Additional Resources​