• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Round to Nearest 10 With round() Function

Python Round to Nearest 10 With round() Function

February 9, 2022 Leave a Comment

In Python, we can round to the nearest 10 easily with the help of the Python round() function. The Python round() function rounds to the nearest whole number, but we can make an adjustment by dividing the input to our function by 10, and then multiplying by 10.

def round_to_nearest_10(x):
    return round(x/10)*10

print(round_to_nearest_10(14))
print(round_to_nearest_10(28))

#Output:
10
30

When working with numbers, rounding can be very valuable when trying to get an approximation or general idea of the scale of a number.

Rounding to the nearest 10 using Python is easy. We can define our own function to round a number to the nearest 10 with the help of the Python built-in round() function.

The round() function by default rounds to the nearest whole number. The trick to rounding to the nearest 10 in Python is to divide the input to the round() function by 10, and then multiply the result by 10.

Below is a function which allows you to round to the nearest 10 in Python.

def round_to_nearest_10(x):
    return round(x/10)*10

print(round_to_nearest_10(14))
print(round_to_nearest_10(28))

#Output:
10
30

How to Round to the Nearest Multiple of Any Number in Python

We can easily generalize our function from above to round to the nearest multiple of any number in Python. To round to the nearest multiple of any number, we just need to divide the input to the round() function by that number, and then multiply the result by that number.

def round_to_nearest(x, num):
    return round(x/num)*num

For example, if we want to round to the nearest hundred, we pass “100” as the second argument to our function.

def round_to_nearest(x, num):
    return round(x/num)*num

print(round_to_nearest(60,100))
print(round_to_nearest(121,100))

#Output:
100
100

If we instead wanted to round to the nearest 33, we would pass “33” as the second argument.

def round_to_nearest(x, num):
    return round(x/num)*num

print(round_to_nearest(60,33))
print(round_to_nearest(121,33))

#Output:
66
132

Hopefully this article has been useful for you to learn how to round to the nearest 10 in Python.

Other Articles You'll Also Like:

  • 1.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 2.  Using Python turtle Module to Draw Square
  • 3.  Writing Multiple Lines Lambda Expression in Python
  • 4.  Length of Set Python – Get Set Length with Python len() Function
  • 5.  How to Measure Execution Time of Program in Python
  • 6.  Python Increment Counter with += Increment Operator
  • 7.  Get pandas Series Last Element in Python
  • 8.  Run Something Every 5 Seconds in Python
  • 9.  Python Replace Space with Underscore Using String replace() Function
  • 10.  Adjusting Python Turtle Screen Size with screensize() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy