4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to round numbers in Python (round, floor, ceil)

By 4Geeks

Round to the nearest number or to decimals with round(), always up or down with math.ceil() and math.floor(), and avoid the surprises with halves and floats.

5 min readLast updated
Round numbers in Python icon

How do you round a number in Python?

Short answer: use round(). round(3.78) returns 4, and round(3.14159, 2) returns 3.14. To always round up, use math.ceil(), and to always round down, use math.floor().

python
import math
 
print(round(3.78))         # 4
print(round(3.14159, 2))   # 3.14
print(math.ceil(3.21))     # 4  always up
print(math.floor(3.78))    # 3  always down

Watch out for exact halves: round(2.5) returns 2, not 3, because Python rounds halves to the nearest even number.

round() goes to the nearest value, halves go to the even number, math.ceil() rounds up and math.floor() rounds down.

Every way to round in Python

What you wantCodeResult
Nearest integerround(3.78)4
Two decimalsround(3.14159, 2)3.14
Always upmath.ceil(3.21)4
Always downmath.floor(3.78)3
Drop the decimalsmath.trunc(-3.7) or int(-3.7)-3
To the nearest hundredround(1234.5678, -2)1200.0
Halves always up (money)Decimal("2.675").quantize(Decimal("0.01"), ROUND_HALF_UP)2.68
Only for displayf"{3.14159:.2f}""3.14"
A NumPy array or pandas columnnp.round(arr, 1), df["col"].round(1)Each value rounded

Round to the nearest integer or to decimals with round()

round(number, ndigits) takes two arguments:

  • number: the value you want to round.
  • ndigits (optional): how many decimals to keep. If you leave it out, you get an integer.
python
price = 19.4567
 
print(round(price))      # 19      int
print(round(price, 2))   # 19.46   float
print(round(price, 0))   # 19.0    float
print(round(1234.5678, -2))  # 1200.0

Without ndigits, round() returns an int. With ndigits, even 0, it returns a float. A negative ndigits rounds to tens, hundreds or thousands.

Career · AI Engineering

You already handle numbers in Python, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Why round(2.5) returns 2

Python rounds exact halves to the nearest even number. This is called banker's rounding, and it avoids pushing totals up when you round many values.

python
print(round(0.5))   # 0
print(round(1.5))   # 2
print(round(2.5))   # 2
print(round(3.5))   # 4
print(round(-2.5))  # -2

There is a second surprise with decimals: round(2.675, 2) returns 2.67, not 2.68. The number 2.675 cannot be stored exactly in binary, so Python actually has something slightly smaller, like 2.67499999....

If you need the rounding you learned at school, where halves always go up, use the decimal module with strings:

python
from decimal import Decimal, ROUND_HALF_UP
 
price = Decimal("2.675")
print(price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))  # 2.68
print(Decimal("2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP))  # 3

Use Decimal for money, invoices and anything where every cent counts.

Round up and round down with math.ceil() and math.floor()

python
import math
 
print(math.ceil(3.21))    # 4
print(math.floor(3.78))   # 3
print(math.ceil(-3.2))    # -3
print(math.floor(-3.7))   # -4
print(math.trunc(-3.7))   # -3
FunctionWhat it does3.7-3.7
math.ceil()Always up4-3
math.floor()Always down3-4
math.trunc() / int()Drops the decimals, toward zero3-3
round()Nearest value4-4

To round up or down to decimals, multiply, round and divide:

python
print(math.ceil(3.14159 * 100) / 100)   # 3.15
print(math.floor(3.14159 * 100) / 100)  # 3.14

Round only when you show the number

Often you want to keep the full value and only show it rounded. Use an f-string:

python
total = 1234567.891
 
print(f"{total:.2f}")   # 1234567.89
print(f"{total:,.2f}")  # 1,234,567.89

The result is text, so do your calculations with the original number.

Round in NumPy and pandas

python
import numpy as np
import pandas as pd
 
print(np.round([1.234, 5.678], 1))           # [1.2 5.7]
print(pd.Series([1.234, 5.678]).round(1).tolist())  # [1.2, 5.7]

df.round(2) rounds every numeric column of a DataFrame. Both libraries also round halves to the nearest even number.

Use AI to check your rounding

An assistant like ChatGPT, Claude or GitHub Copilot can explain a surprising result and suggest the right method. These three prompts are ready to copy.

1. Explain a result

In Python, round([your number], [decimals]) returns [the result] and I expected [what you expected].
Explain why and show me how to get the result I want.

2. Choose a method

I need to round [prices / grades / measurements] in Python to [number] decimals,
always [up / down / to the nearest]. Show me the best method and explain why.

3. Round a dataset

I have a pandas DataFrame with these columns: [list the columns].
Show me how to round [the columns] to [number] decimals without changing the others.

Run every answer yourself and compare the results with a few values you know.

Learn with a mentor

From rounding numbers to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems when rounding in Python

What you seeWhy it happensFix
round(2.5) returns 2Python rounds halves to the nearest even numberUse Decimal with ROUND_HALF_UP
round(2.675, 2) returns 2.672.675 is stored as a slightly smaller binary numberUse Decimal("2.675")
0.1 + 0.2 shows 0.30000000000000004Floats are stored in binaryround(0.1 + 0.2, 2) or an f-string
TypeError: type str doesn't define __round__ methodThe value is text, for example from input()Convert it: round(float(value), 2)
round(3.7, 0) returns 4.0, not 4With ndigits, round() returns a floatLeave out ndigits, or use int()
int(3.99) returns 3int() drops the decimals, it does not roundUse round()
math.floor(-3.7) returns -4Floor always goes down, also with negativesUse math.trunc() to go toward zero
Free download

Rounding in Python in a 2-page cheat sheet

round(), math.ceil(), math.floor() and math.trunc(), halves and floats, Decimal for money, display formats, NumPy and pandas, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to round in Python cheat sheet

2 pages · PDF · 4Geeks

  • The right function for nearest, up, down or toward zero
  • Why round(2.5) returns 2 and how to change it
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about rounding in Python

Want to go from Python basics to building AI apps that work with real data, with mentors?

See the AI Engineering program