# Python – Round to 5 (or other number) in Python

Posted on

python, rounding

Is there a built-in function that can round like the following?

`10 -> 1012 -> 1013 -> 1514 -> 1516 -> 1518 -> 20`

## Related posts

- .net – vb.net: rounding to next whole number
- Python – Round to 5 (or other number) in Python
- Python – Rounding to two decimal places in Python 2.7
- Python – How to round up a complex number
- Python – Prevent Rounding to Zero in Python
- Python – Round a floating-point number down to the nearest integer
- C# – rounding decimal number in linq
- Python – How to round UP a number
- Java – Rounding to 6 decimal places using Math.round method in Java android
- Javascript – Rounding a number to one decimal in javascript
- Rounding to Nearest Even Number in C++
- Python – rounding errors in Python floor division
- Python – round() doesn’t seem to be rounding properly
- Python – How to round float 0.5 up to 1.0, while still rounding 0.45 to 0.0, as the usual school rounding
- Python – Rounding up to nearest 30 minutes in python
- C# – Why does .Net use a rounding algorithm in String.Format that is inconsistent with the default Math.Round() algorithm
- Javascript – Rounding issue in Math.round() & .toFixed()
- Xml – XSL – rounding/format-number problem
- Objective-c – Rounding down with a number. Finding the n in n.mp
- Python – How to avoid incorrect rounding with numpy.round
- Rounding off a number in verilog
- Python – Better rounding in Python’s NumPy.around: Rounding NumPy Arrays
- Java – Rounding off 2 decimal places in Java for whole number
- Format a number with comma separators and round to 2 decimal places in Python 2
- Haskell – Rounding to specific number of digits in Haskell
- Javascript – Rounding a percentage to the nearest whole number
- Php – Always display specified number of decimal places in PHP (rounding up)
- Python – Rounding time in Python
- Javascript – rounding to an arbitrary number of significant digits in javascript is not working
- C# – Stop Rounding In C# At A Certain Number
- Javascript – Truncate number to two decimal places without rounding
- Python – Round without round function python
- C++ – How to truncate a floating point number after a certain number of decimal places (no rounding)
- Python rounding a floating point number to nearest 0.05
- Java – Format number with JSTL and no rounding
- Python – Rounding a list of floats into integers in Python
- Vb.net – Rounding a number down in Visual Basic
- Python – Jinja2 round filter not rounding
- C# – Using Decimal.ToString() without rounding up the number
- Swift – Rounding a double value to x number of decimal places in swift
- Ruby Big Decimal Rounding with fixed number
- Java – Rounding up a number to nearest multiple of 5
- C++ – Rounding a double number up to the tenths place
- Excel rounding up or down to specific number
- Java – Can “java.lang.ArithmeticException: Rounding necessary” is detected before rounding
- How to round up a number to the nearest 10 in python 3.4.0
- Vb.net – Prevent Math.Round(95.55555555,2) from rounding to 95.56 in VB.NET
- Rounding to an arbitrary number of significant digits
- Python – How to round a floating point number up to a certain decimal place
- Javascript – Rounding a number to exactly two decimal places for currency formatting
- C++ – Rounding off a positive number to the next nearest multiple of 5
- Ruby – Truncate a floating point number without rounding up
- Python – Rounding a number in Python but keeping ending zeros
- Python – How to ONLY round a number/float down in Python
- Python – Rounding positive/negative numbers to nearest “whole” number
- C++ – Rounding up and down a number C++
- Javascript – Using toFixed(2) and math round to get correct rounding
- Python – Round a value to nearest number divisible by 2, 4, 8 and 16
- Python – Rounding a number while using string formatting in python
- Rounding Floating Point Numbers after addition (guard, sticky, and round bits)
- Python – How to round a number to significant figures in Python
- Vb.net – Rounding to a whole number in VB.NET
- Java – Android/Java: Rounding up a number to get no decimal
- Javascript – Prevent JavaScript Number function from rounding big numbers
- Python – datetime: Round/trim number of digits in microseconds
- Rounding numbers in R to specified number of digits
- C++ – How to display a fixed number of digits in C++ without rounding
- Python – Round number to nearest integer
- Python – how to round a float number to the nearest integer in Python
- C# – How to show number without rounding off numbers
- Sql – Rounding to a specific number of significant digits in Oracle
- Angularjs – rounding a number in input but keep full decimal value in model
- Java – Rounding a number up in Java
- Delphi – SetRoundMode(rmUp) and rounding “round” values like 10, results in 10,0001 how come
- C# Rounding 2 decimals: Truncate vs Round
- Ios – Rounding float number upto two decimal places
- SSRS 2005 Round(foo, 0) is rounding a decimal value of 12.6 to 12 instead of 13
- Python 2.7: round a float up to next even number
- Objective-c – Rounding a float number in objective-c
- Javascript – How to round using ROUND HALF UP. Rounding mode that most of us were taught in grade school
- C++ – How to do floating point rounding with a bias (always round up or down)
- Java – Rounding Down to nearest whole number – am I cheating or is this more than adequate
- Issue with rounding number in robotframework
- Batch rounding a number
- Java – why is java math.round always rounding down?
- Xml – Number rounding and precision problems in XSLT 1.0
- Objective-c – Rounding up to the nearest whole number
- Php – number_format is rounding final decimal numbers and increasing to next round decimal number
- Swift – Rounding in Swift with round()
- C++ – Rounding to use for int -> float -> int round trip conversion
- C++ – Rounding up to the nearest multiple of a number
- Matlab – Rounding a decimal number in MATLAB

## Best Solution

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

## Python 3

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.