The math.ceil (ceiling) function returns the smallest integer higher or equal to x.

For Python 3:

import math
print(math.ceil(4.2))

For Python 2:

import math
print(int(math.ceil(4.2)))
Answer from Steve Tjoa on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_round.asp
Python round() Function
The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_floor.asp
Python math.floor() Method
# Import math library import math ... returns the result. Tip: To round a number UP to the nearest integer, look at the math.ceil() method....
Top answer
1 of 15
572

TL;DR:

round(x)

will round it and change it to integer.

You are not assigning round(h) to any variable. When you call round(h), it returns the integer number but does nothing else; you have to change that line for:

h = round(h)

to assign the new value to h.


As @plowman said in the comments, Python's round() doesn't work as one would normally expect, and that's because the way the number is stored as a variable is usually not the way you see it on screen. There are lots of answers that explain this behavior.

One way to avoid this problem is to use the Decimal as stated by this answer.

In order for this answer to work properly without using extra libraries it would be convenient to use a custom rounding function. I came up with the following solution, that as far as I tested avoided all the storing issues. It is based on using the string representation, obtained with repr() (NOT str()!). It looks hacky but it was the only way I found to solve all the cases. It works with both Python2 and Python3.

def proper_round(num, dec=0):
    num = str(num)[:str(num).index('.')+dec+2]
    if num[-1]>='5':
        return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
    return float(num[:-1])

Tests:

>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>> 
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0

Finally, the corrected answer would be:

# Having proper_round defined as previously stated
h = int(proper_round(h))

Tests:

>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1  # should be 7

The gotcha here is that the dec-th decimal can be 9 and if the dec+1-th digit >=5 the 9 will become a 0 and a 1 should be carried to the dec-1-th digit.

If we take this into consideration, we get:

def proper_round(num, dec=0):
    num = str(num)[:str(num).index('.')+dec+2]
    if num[-1]>='5':
      a = num[:-2-(not dec)]       # integer part
      b = int(num[-2-(not dec)])+1 # decimal part
      return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
    return float(num[:-1])

In the situation described above b = 10 and the previous version would just concatenate a and b which would result in a concatenation of 10 where the trailing 0 would disappear. This version transforms b to the right decimal place based on dec, as a proper carry.

2 of 15
30

Use round(x, y). It will round up your number up to your desired decimal place.

For example:

>>> round(32.268907563, 3)
32.269
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-round-numbers-up-or-down-in-python
Python Round to Int โ€“ How to Round Up or Round Down to the Nearest Whole Number
May 24, 2022 - We'll also see how to use the function's parameters to change the type of result returned to us. We'll then talk about the math.ceil() and math.floor() methods which rounds up and rounds down a number to the nearest whole number/integer respectively.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-round-up
How to Round Up a Number in Python | DataCamp
July 22, 2024 - You can use the math.ceil() function from the math module to round up to the nearest int in Python. Rounding bias occurs when there are inaccurate values due to distortion of numbers when rounding.
๐ŸŒ
Real Python
realpython.com โ€บ python-rounding
How to Round Numbers in Python โ€“ Real Python
December 7, 2024 - In Python, math.ceil() implements the ceiling function and always returns the nearest integer thatโ€™s greater than or equal to its input: ... Notice that the ceiling of -0.5 is 0, not -1. This makes sense because 0 is the nearest integer to -0.5 thatโ€™s greater than or equal to -0.5. Now write a function called round_up() that implements the rounding up strategy:
๐ŸŒ
Inspector
inspector.dev โ€บ home โ€บ round up numbers to integer in python โ€“ fast tips
Round Up Numbers to Integer in Python - Inspector.dev
June 17, 2025 - The math library in Python provides the ceil() and floor() functions to round numbers up and down, respectively, to the nearest integer. These functions are useful when you need to work with integer values, especially in cases like calculating ...
Find elsewhere
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-round-up-a-number-to-the-nearest-integer-in-python-398061
How to round up a number to the nearest integer in Python | LabEx
This can be particularly useful ... approach is preferred. In Python, the math.ceil() function from the math module can be used to round a number up to the nearest integer....
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3288132 โ€บ how-to-round-a-number-to-the-nearest-integer-in-python
How to round a number to the nearest integer in python? | Sololearn: Learn to code for FREE!
to get it done, we can use an f-string like this: val = 42.1 print(f'{val:.2f}') # for more details have a look at the python docs result is: 42.10 ... I would not suggest using the round() method to get the nearest integer, it is because the ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_math_ceil.asp
Python math.ceil() Method
The math.ceil() method rounds a number UP to the nearest integer, if necessary, and returns the result.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ round-function
Mimo: The coding platform you need to learn Web Development, Python, and more.
Start your coding journey with Python. Learn basics, data types, control flow, and more ... 1. Round to the Nearest Integer: When called with one argument, round() returns the nearest whole number.
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-round-function-tutorial
Python Round() Function Tutorial - Server Academy
In this tutorial, weโ€™ll cover everything you need to know about the round() function, including how to use it, how to round up or down, and practical examples for common rounding tasks.
๐ŸŒ
Thomascollart
thomascollart.com โ€บ python-round-numbers
How to Round Numbers in Python? | Thomas Collart
November 21, 2023 - In Python, rounding is done using the round() function. The round() function rounds a decimal number to the nearest integer. It also rounds floating-point numbers to a specified decimal place.
๐ŸŒ
Medium
medium.com โ€บ 4geeksacademy โ€บ how-to-round-in-python-cf547f8c9376
How to Round in Python? | by 4Geeks Academy | 4GeeksAcademy | Medium
June 20, 2023 - Here is a code snippet that shows how to round a number in Python: number = 3.78 rounded_number = round(number) print(rounded_number) # Output: 4 ยท In the previous example we rounded to the nearest integer number the variable number which value was 3.78. The result is the stored in rounded_number and it is printed in the console as 4. This process implies to get a specific float number into the nearest integer number or even to a specific precision of decimals.
๐ŸŒ
Afternerd
afternerd.com โ€บ blog โ€บ round-number-nearest-integer
Python: Round a Number to the Nearest Integer - Afternerd
February 11, 2021 - You can use the Round built-in function in Python to round a number to the nearest integer.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-round-numbers-in-python
How to Round Numbers in Python? - GeeksforGeeks
April 30, 2025 - Explanation: Same shifting logic as round up, but using floor(). NumPy provides the np.round() function for rounding arrays or numbers efficiently. ... import numpy as np arr = np.array([1.234, 2.567, 3.789]) print("Nearest integer:", np.round(arr)) ...
๐ŸŒ
DEV Community
dev.to โ€บ kiani0x01 โ€บ python-round-to-nearest-integer-p76
Python Round to Nearest Integer - DEV Community
July 22, 2025 - Round numbers to the nearest integer in Python with round(), math, Decimal, and numpy while managing tie-breaking and precision.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ python โ€บ python round function: rounding numbers in python
Python Round Function: Rounding Numbers in Python
April 1, 2025 - ... This technique always rounds a number down to a number of digits smaller than or equal to the given number. The Python math module has the floor() function that rounds a number to the nearest integer, smaller than or equal to the given number.