We will learn how to round a number up to the nearest integer in python using three different methods. Each method is simple, and you can choose whichever suits you most.
Round a number up to the nearest integer in python
Method 1: Using the round()
Syntax:
round(number)
Parameter:
- number: Required. The number is to be rounded.
The round()
function returns a number rounded to the nearest whole number. However, because .5 is out of range for binary floating-point numbers. As a result, the round()
function may give you unexpected results:
a = 3.5 b = 4.5 print(a, b) c = round(a) d = round(b) print(c, d)
Output:
3.5 4.5
4 4
Computers use a binary floating-point format for internally storing numbers. This means that computers can’t accurately represent numbers like 0.1, 0.2, or 0.3 at all. Instead, you’ll need to research why computers use this format. Just remember, the round()
function rounds the values .5 to the nearest even integer so you need to deal with what to do when the value contains the .5 fraction part.
Method 2: Using math.ceil()
The math.ceil()
method takes a numeric expression as a parameter. Here’s what the syntax looks like:
Syntax:
math.ceil(number)
Parameter:
- number: the value to round up.
The math.ceil()
function returns the rounded up value of the passed parameter, which is the nearest integer equal to or larger than that argument’s value. You can use the value returned by the function to round a number up to its nearest integer:
import math print(math.ceil(1.00)) print(math.ceil(1.05)) print(math.ceil(1.75))
Output:
1
2
2
The ‘math’ module is first imported in the code above. This allows us to use all the methods included in the math module. This function always rounds up to a full integer.
Method 3: Using math.floor()
Here’s the syntax of it:
Syntax:
math.floor(number)
Parameter:
- number: the value to round down.
The math.floor()
function returns a rounded integer value of the parameter, which is the nearest number equal to or smaller than the passed argument’s value. You can use the value returned by the function and then plus 1 to round a number up to its nearest integer:
import math print(math.floor(1.00) + 1) print(math.floor(1.05) + 1) print(math.floor(1.75) + 1)
The math.floor()
function simply rounds down the number passed.
Output:
2
2
2
The module math is also imported in the code above. This gives us the ability to use all the methods included in the math module. To round a number up to the nearest integer, all we need to do is use this function.
Summary
We have learned how to round a number up to the nearest integer in python using three different methods. Although we have many ways to cope with this type of problem, we hope you can sort out the optimized way which suits you best.
Maybe you are interested:
- Check if a number is divisible by another number in Python
- Force division to return a floating-point number in Python
- Get the length of an Integer in Python

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R