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.

Answer from francisco sollima on Stack Overflow
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
Discussions

Round function should be improved
the round function should be improved, the round function is supposed to round up numbers that are in a decimal format. I have found that the round function only looks at the first decimal point, for example when I enter this: print(round(1.45), round(1.54)) as a result I get: 1 2 I have created ... More on discuss.python.org
🌐 discuss.python.org
0
July 5, 2024
How can I correctly round these values to the nearest integer using Python round to int? - TestMu AI Community
How can I use python round to int for rounding numbers to the nearest integer? I’ve been trying to round long float numbers, such as: 32.268907563 32.268907563 31.2396694215 33.6206896552 … But I’ve had no success so … More on community.testmuai.com
🌐 community.testmuai.com
0
December 2, 2024
Round off -0.5 to nearest integer
There are a lot of different rounding methods . I usually round half away from zero because that makes round(-x)=-round(x) which I consider to be a useful property. Check with your teacher which method they will consider as correct for any mark schemes. More on reddit.com
🌐 r/askmath
42
45
March 16, 2024
Rounding a decimal/float to an integer in Python?
round rounds to the overall nearest integer. math.floor rounds to the nearest integer that's smaller than the input. math.ceil rounds to the nearest integer that's larger than the input: round(0.3) # -> 0 round(0.7) # -> 1 math.floor(0.3) # -> 0 math.floor(0.7) #- > 0 math.ceil(0.3) # -> 1 math.ceil(0.7) # -> 1 More on reddit.com
🌐 r/learnpython
1
2
January 28, 2022
🌐
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. ... If ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › round-function-python
round() function in Python - GeeksforGeeks
import math num = 3.6 rounded_num = math.floor(num) # rounds down to nearest integer print(rounded_num) # output: 3 rounded_num = math.ceil(num) # rounds up to nearest integer print(rounded_num) # output: 4 ... In this example, we are using ...
Published   August 7, 2024
🌐
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.
🌐
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 simplest way to round a number in Python is to use the built-in round() function. The round() function takes a number as the first argument and an optional second argument to specify the number of decimal places to round to.
🌐
Server Academy
serveracademy.com › blog › python-round-function-tutorial
Python Round() Function Tutorial - Server Academy
Round Down: Use math.floor() to always round down to the nearest integer. Bankers’ Rounding: When rounding halfway cases, Python rounds to the nearest even integer by default.
Find elsewhere
🌐
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - Then look at the digit d in the first decimal place of m. If d is less than 5, round m down to the nearest integer. Otherwise, round m up. Finally, shift the decimal point back p places by dividing m by 10ᵖ.
🌐
Python.org
discuss.python.org › python help
Round function should be improved - Python Help - Discussions on Python.org
July 5, 2024 - the round function should be improved, the round function is supposed to round up numbers that are in a decimal format. I have found that the round function only looks at the first decimal point, for example when I enter this: print(round(1.45), ...
🌐
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.
🌐
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 - When we passed in the number variable to the round() function, it got rounded to the nearest whole number which is 3.
🌐
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!
August 13, 2024 - [ Laur€nt ] , in python there is no such function. 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 · 13th Aug 2024, 12:42 PM · Lothar · + 4 · I would not suggest using the round() method to get the nearest integer, it is because the round() method uses banker's rounds, which can give an expected (for normal use) result.
🌐
DataCamp
datacamp.com › tutorial › python-round-up
How to Round Up a Number in Python | DataCamp
July 22, 2024 - # Import the numpy module and alias it as np for convenience import numpy as np # Create a numpy array with floating-point numbers array = np.array([3.14, 2.72, 1.61]) # Apply the ceiling function to each element in the numpy array, rounding each number up to the nearest integer rounded_up_array = np.ceil(array) print(rounded_up_array) # 4, 3, 2 · Here is a table comparing different methods for rounding up in Python:
🌐
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.
🌐
FavTutor
favtutor.com › blogs › round-down-python
Round Down Numbers in Python (With Examples and Code)
September 3, 2022 - Python's floor division operator, aka the integer division operator, is like math.floor() method. It divides the first number by the second and then rounds down the result to the nearest lower integer.
🌐
TestMu AI Community
community.testmuai.com › ask a question
How can I correctly round these values to the nearest integer using Python round to int? - TestMu AI Community
December 2, 2024 - How can I use python round to int for rounding numbers to the nearest integer? I’ve been trying to round long float numbers, such as: 32.268907563 32.268907563 31.2396694215 33.6206896552 … But I’ve had no success so far. I tried using math.ceil(x) and math.floor(x) (although these roundup or down, which is not what I’m looking for), and I also tried round(x), but it still resulted in float numbers.
🌐
Hyperskill
hyperskill.org › university › python › rounding-and-round-in-python
Rounding and round() in Python
August 2, 2024 - Python provides several rounding methods for handling cases where the fractional part is exactly halfway between two integers. These rounding methods are implemented in the built-in round() function and the Decimal class. ROUND_HALF_UP: Rounds towards the nearest integer and if the fractional ...
🌐
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
In Python, the process of rounding a number can be performed using the following built-in functions: round(): This function rounds a number to the nearest integer or to a specified number of decimal places.
🌐
Sabe
sabe.io › blog › python-round-numbers
How to Round Numbers in Python
December 2, 2022 - Simply pass in your number and it will return the nearest integer. PYTHONnumber = 3.14159 rounded = round(number) print(rounded)