Here's how to round a number to the nearest tenth:

>>> round(21.3331, 1)
21.3

Here's how to print out a floating point number with one decimal point:

>>> print "%.1f" % (21.3331,)
21.3

Note that if you do it using %d it will get printed as rounded to the nearest integer:

>>> print "%d" % (21.3331,)
21

See the String Formatting Operations docs for more on how the % formatting works.

Answer from Claudiu on Stack Overflow
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ 2 ways to round a float to nearest 10th
2 Ways to Round a Float to Nearest 10th - AskPython
August 27, 2023 - To round off a number to its 10th position, we check if the number after it (the digit in the hundredth place) is greater than or equal to 5. If it is so, we add one to the number in the tenth digit.
People also ask

What is the Python round function?
The Python round function is a built-in function that rounds a number to a specified number of decimal places or to the nearest integer. It helps maintain numerical precision, making calculations and data presentation cleaner and more accurate in Python programs.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ blog โ€บ data science โ€บ understanding python round function: guide to precise rounding
Round Function in Python [2025] โ€“ Explained for Beginners
How to use round function in Python with decimal places?
You can specify decimal places using the second argument. For instance, round(3.14159, 2) returns 3.14. This is useful when displaying currency, scientific data, or percentages where consistent precision is required for readability and accuracy.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ blog โ€บ data science โ€บ understanding python round function: guide to precise rounding
Round Function in Python [2025] โ€“ Explained for Beginners
How to round numbers in a list using Python?
You can use a list comprehension: [round(num, 2) for num in numbers]. This applies the Python round function to each element in a list, making it efficient for datasets, arrays, or repeated calculations in data analysis.
๐ŸŒ
upgrad.com
upgrad.com โ€บ home โ€บ blog โ€บ data science โ€บ understanding python round function: guide to precise rounding
Round Function in Python [2025] โ€“ Explained for Beginners
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ python โ€บ python-round โ€บ python-round-to-nearest-10
Python - Round Number to Nearest 10
November 30, 2020 - Python - To round number to nearest 10, use round() function. We can divide the value by 10, round the result to zero precision, and multiply with 10 again. Or you can pass a negative value for precision.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_func_round.asp
Python round() Function
The default number of decimals is 0, meaning that the function will return the nearest integer. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
Find elsewhere
๐ŸŒ
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
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ blog โ€บ data science โ€บ understanding python round function: guide to precise rounding
Round Function in Python [2025] โ€“ Explained for Beginners
October 8, 2025 - If itโ€™s negative, the rounding happens to the left of the decimal (for example, to the nearest ten or hundred). From foundational certifications to advanced degrees, gain cutting-edge skills in Generative AI, Machine Learning, and Data Analysis. Enrol now and lead the change. Masterโ€™s Degree in Artificial Intelligence and Data Science From OPJGU ยท Generative AI Mastery Certificate for Data Analysis ... In the first line, the number 12.567 is rounded to two decimal places. In the second line, Python rounds it to the nearest integer because the ndigits argument isnโ€™t specified.
๐ŸŒ
Real Python
realpython.com โ€บ python-rounding
How to Round Numbers in Python โ€“ Real Python
December 7, 2024 - The rounding half up strategy rounds every number to the nearest number with the specified precision and breaks ties by rounding up. Here are some examples: To implement the rounding half up strategy in Python, you start as usual by shifting the decimal point to the right by the desired number of 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 - For debugging your python application you can take a look at Inspector. Rounding decimals refer to the rounding of decimal numbers to a certain degree of accuracy. We can round decimals to the nearest wholes, tenths or hundredths.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-round-number-to-nearest-100
Round a number to the nearest 5, 10, 100, 1000 in Python | bobbyhadz
Divide the result by 2 to get the number rounded up to the nearest 0.5. Use the math.floor() method to round a number down to the nearest 0.5.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python round up to the nearest ten
How to Round Up to the Nearest Ten in Python | Delft Stack
February 2, 2024 - Consequently, the output of the code is 20, representing the result of rounding 21 down to the nearest ten using the floor() function. The round() function is a Python built-in function that is used for rounding a number to a specified number ...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-round-numbers-in-Python
How to round numbers in Python - Quora
Answer (1 of 11): Use [code ]math.floor[/code] to round down, math.ceil to round up and [code ]round[/code] to round to nearest integer. [code]>>> math.floor(4.4), math.floor(4.5), math.floor(5.4), math.floor(5.5) (4.0, 4.0, 5.0, 5.0) >>> round(4.4), round(4.5), round(5.4), round(5.5) (4.0, 5.0,...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ built-in โ€บ round
Python round()
Become a certified Python programmer. Try Programiz PRO! ... The round() function rounds a number. ... # round 13.46 to the nearest integer rounded_number = round(number) print(rounded_number) # Output: 13
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
๐ŸŒ
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.