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
573

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
🌐
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.
Discussions

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
How to round numbers below?
Use math.floor() to round down. https://docs.python.org/3/library/math.html#math.floor More on reddit.com
🌐 r/learnpython
7
0
November 29, 2024
How to round a value in a print statement to the nearest 100?
To display a number rounded to the nearest hundred, with two digits after the decimal: print('{:0.2f}'.format(round(x, -2))) Tested: >>> x = 157395.85 >>> print('{:0.2f}'.format(round(x, -2))) 157400.00 More on reddit.com
🌐 r/learnpython
11
4
January 30, 2019
How to round to the nearest 0.5 in python?

This should work: round(x*2)/2

More on reddit.com
🌐 r/learnprogramming
5
9
October 28, 2014
🌐
Server Academy
serveracademy.com › blog › python-round-function-tutorial
Python Round() Function Tutorial - Server Academy
The round() function in Python rounds a floating-point number to the nearest integer or specified number of decimal places.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › round-function-python
round() function in Python - GeeksforGeeks
By default, round() rounds a number to the nearest integer. However, you can also specify whether to round up or down using the round() function in combination with the math module.
Published   August 7, 2024
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › python-round-up
How to Round Up a Number in Python | DataCamp
July 22, 2024 - Python uses the math module, NumPy, and Pandas libraries to offer different methods of rounding up numbers. The math.ceil() function from math is used to round up a number to the nearest integer.
Find elsewhere
🌐
TradingCode
tradingcode.net › python › math › round-integers
How to round to whole numbers in Python?
To explore how the math.floor() function works in practice, let’s examine the following Python program: import math # Some random values valueA = 11.2829850 valueB = 19.2545879 valueC = 0.50000001 valueD = 34.6403001 valueE = -9.9121138 # ...
🌐
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 - To use the above example. def my_rounding(num): '''It only handles positive integer''' q, r = divmod(num, 1) if r >= 0.5: return int(q + 1) else: return (q) print(my_rounding(2.5)) # 3 print(my_rounding(2.4)) # 2 https://www.sololearn.com/discuss/3284453/?ref=app ... Gulshan Mahawar Wong Hei Ming Lothar Thank you, thanks to you I was able to solve my coach code(⁠づ⁠。⁠◕⁠‿⁠‿⁠◕⁠。⁠)⁠づ༼⁠ ⁠つ⁠ ⁠◕⁠‿⁠◕⁠ ⁠༽⁠つ ... ***JUMP_LINK__&&__Python__&&__JUMP_LINK , do not post a new question in this existing discussion.
🌐
Afternerd
afternerd.com › blog › round-number-nearest-integer
Python: Round a Number to the Nearest Integer - Afternerd
July 22, 2025 - You can use the Round built-in function in Python to round a number to the nearest integer.
🌐
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ᵖ.
🌐
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 28, 2025 - In this article, we talked about three built-in functionalities in Python that let us round numbers. The round() function rounds a number to the nearest whole number.
🌐
Mimo
mimo.org › glossary › python › round-function
Python round(): Rounding Numbers in Python
Using a negative value for the ndigits parameter, the round() function can round a floating-point number or integer to a multiple of 10. In the following example, we round 12345 to the nearest hundred (12300), simplifying the value. ... Python’s round() function works well in many cases.
🌐
DEV Community
dev.to › kiani0x01 › python-round-to-nearest-integer-p76
Python Round to Nearest Integer - DEV Community
November 21, 2023 - Python’s built-in round() function is the first tool most developers reach for. Its basic form is simple: value = round(2.7) # returns 3 value2 = round(2.3) # returns 2 · By default, it returns an integer when no precision is given.
🌐
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.
🌐
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.
🌐
Milddev
milddev.com › python-round-to-nearest-integer
Python Round to Nearest Integer
July 16, 2025 - Round numbers to the nearest integer in Python with round(), math, Decimal, and numpy while managing tie-breaking and precision.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-round-numbers-in-python
How to Round Numbers in Python? - GeeksforGeeks
July 15, 2025 - Nearest integer: [1. 3. 4.] Two decimals: [1.23 2.57 3.79] ... When rounding large datasets, we must avoid rounding bias. Python supports several rounding methods for that and most generally used methods are: