I don't know of a standard function in Python, but this works for me:

Python 3

def myround(x, base=5):
    return base * round(x/base)

It is easy to see why the above works. You want to make sure that your number divided by 5 is an integer, correctly rounded. So, we first do exactly that (round(x/5)), and then since we divided by 5, we multiply by 5 as well.

I made the function more generic by giving it a base parameter, defaulting to 5.

Python 2

In Python 2, float(x) would be needed to ensure that / does floating-point division, and a final conversion to int is needed because round() returns a floating-point value in Python 2.

def myround(x, base=5):
    return int(base * round(float(x)/base))
Answer from Alok Singhal on Stack Overflow
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ round number to the nearest multiple in python (2, 5, 10, etc.)
Round Number to the Nearest Multiple in Python (2, 5, 10, etc.) โ€ข datagy
April 13, 2023 - # Developing a function to round to a multiple from math import ceil, floor def round_to_multiple(number, multiple, direction='nearest'): if direction == 'nearest': return multiple * round(number / multiple) elif direction == 'up': return multiple * ceil(number / multiple) elif direction == 'down': return multiple * floor(number / multiple) else: return multiple * round(number / multiple) # Rounding 23 to a multiple of 5 print(round_to_multiple(23, 5, 'nearest2')) print(round_to_multiple(23, 5, 'up')) print(round_to_multiple(23, 5, 'down')) # Returns: # 25 # 25 # 20 ยท Here, we modified our fu
๐ŸŒ
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. At this point, though, you need a way to determine if the digit just after the shifted decimal point is less than or greater than or equal to 5.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ python โ€บ python round up practical examples [5 methods]
Python round up practical examples [5 Methods] | GoLinuxCloud
November 16, 2021 - In this section, we will round a number to the nearest 5. See the python program below: ... # defining a number num = 4343 # Python round up number to nearest 5 rounded = 5 * round(num/5) # printing print("The number round up to nearest 5 is ...
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-round-number-to-nearest-100
Round a number to the nearest 5, 10, 100, 1000 in Python | bobbyhadz
Multiply the result by 5 to get the number rounded to the nearest 5. Use the math.ceil() method to round a number up to the nearest 5.
๐ŸŒ
Server Academy
serveracademy.com โ€บ blog โ€บ python-round-function-tutorial
Python Round() Function Tutorial - Server Academy
The math.ceil() function rounds a number up to the nearest integer, regardless of its decimal value. import math print(math.ceil(4.2)) # Rounds up to 5 print(math.ceil(4.7)) # Also rounds up to 5
๐ŸŒ
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 - We can round decimals to the nearest wholes, tenths or hundredths. To round a number look at the next digit in the right place, if the digit is less than 5, round down and if the digit is 5 or more than 5, round up...
Find elsewhere
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2660464 โ€บ print-the-nearest-multiple-of-5-in-python
Print the nearest multiple of 5. In python | Sololearn: Learn to code for FREE!
Divide the number by 5, round it to zero decimal places, then multiply by 5. Done! ... Your attempt is needed Hint : modulo(%) operator can be used to find remainder after division.
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ round-to-nearest-multiple-of-5
How to Round a Number to the Nearest Multiple of 5 (Up or Down) - CodingTechRoom
# Python function to round to nearest multiple of 5 def round_to_nearest_five(num): return round(num / 5) * 5 # Example usage: numbers = [12.5, 62.1, 68.3, 74.5, 80.7] rounded_numbers = [round_to_nearest_five(num) for num in numbers] print(rounded_numbers) # Output: [10, 60, 70, 75, 80]
๐ŸŒ
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.
๐ŸŒ
KnowledgeHut
knowledgehut.com โ€บ home โ€บ blog โ€บ data science โ€บ understanding python round function: guide to precise rounding
How to Round Numbers in Python? With Examples
October 8, 2025 - If ndigits is zero or not given, Python rounds to the nearest whole number. If ndigits is negative, it rounds to the left of the decimal (to the nearest ten, hundred, etc.). You can easily round to 1, 2, or 3 decimal places depending on your ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ round-function-python
round() function in Python - GeeksforGeeks
Similarly, round(-4.7) returns -5 since -5 is closer to -4.7 than -4. Similarly round(-2.5) returns -2 because it rounds down when the decimal part is exactly 0.5. Same as the fourth example demonstrates using the ndigits parameter with a negative number. round(-2.675, 2) returns -2.67.Similarly, round(-1234, -2), returns -1200 because it rounds to the nearest hundred, which is in the negative direction.
Published ย  August 7, 2024
๐ŸŒ
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 - In order to round this number up ... the code above, is 6. Just like we did in the last section, in order to use the math.floor() method, we must first import the math module....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ [pandas] how to round up to the nearest 0.5?
r/learnpython on Reddit: [pandas] How to round up to the nearest 0.5?
January 5, 2022 -

I have a dataframe df:

df = pd.DataFrame({"volume": [0.3300, 5.600, 64.0915, 1.730000, 4.123000]})
volume
0.3300
5.600
64.0915
1.730000
4.123000

I also have a non-exhausting dict di:

di = {
    0.5: 6.26,
    1.0: 6.28,
    1.5: 6.36,
    2.0: 6.46,
    2.5: 6.56,
    3.0: 6.66,
    3.5: 6.76,
    4.0: 6.86,
    4.5: 6.96,
    5.0: 6.98,
    5.5: 7.15
    ...
}

I need to create a new column ["map"] where I map di to df["volume"].

df["map"] = df["volume"].map(di)

but for that I need to round up each number in df["volume"] to the next 0.5, so the values should look like:

volumevolume_round_up
0.33000.5
5.6006.0
64.091564.5
1.7300002.0
4.1230004.5

How can I do this in a vectorized way?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-round-numbers-in-python
How to Round Numbers in Python? - GeeksforGeeks
July 15, 2025 - In this article, we'll cover the most commonly used techniques for rounding numbers in Python, along with examples. For example, Input is 3.5 then Output should be 4. Pythonโ€™s built-in round() function rounds a number to a given precision. If no precision is specified, it rounds to the nearest ...