How to Round Numbers in Python

To round numbers in Python, use the built-in round() function.

For example, let’s round a floating-point number to its nearest integer value:

x = round(2.33453)
print(x)

Output:

2

To round up or down, use the math module’s ceil() or floor() functions:

import math

up   = math.ceil(2.4213)   # Returns 3
down = math.floor(2.4213)  # Returns 2

In this guide, you will learn how to:

  • Round a decimal number to a certain number of decimals.
  • Round a number up.
  • Round a number down.

How to Round to 2 Decimals in Python

To specify how many decimals you want to round, give a second argument to the round() function.

For example, let’s round a decimal to two decimal places:

x = round(2.33453, 2)
print(x)

Output:

2.33

How to Round a Number Up to the Nearest Integer in Python

To round a number up to the nearest integer, use the math module’s ceil() function.

For example:

import math

x = math.ceil(2.4213)
print(x)

Output:

3

Even though 2.4213 is closer to 2 than 3, it is rounded to 3 because we want to round it up.

As another demonstrative example, let’s round the number 1.0001 up:

import math

x = math.ceil(1.0001)
print(x)

Output:

2

How to Round a Number Down to the Nearest Integer in Python

To round a number down to the nearest integer, use the math module’s floor() function.

For example:

import math

x = math.floor(2.92)
print(x)

Output:

2

Notice how even though the number 2.92 is pretty close to 3, it still gets rounded down. This is intentional, as the floor function rounds the number down regardless of how close it is to the next integer above it.

Conclusion

Today you learned how to round numbers in Python, use the round() function.

To round up to the nearest integer, use math.ceil(). To round down to the nearest integer, use math.floor().

import math

x = math.ceil(2.4213)
y = math.floor(2.4213)

print(x, y)
# Prints 3 2

To round a number to several decimals, give the round() function a second argument.

x = round(2.33453, 2)
print(x)

Thanks for reading. I hope you find it useful.

Happy coding!

Scroll to Top