How To Round A Number To The Nearest 5 In Python

Round a number to the nearest 5 in Python

To round a number to the nearest 5 in Python, we can use ways like customizing a function using Python’s built-in functions and using the math.ceil() function.

Round a number to the nearest 5 in Python

Customize a function that returns the nearest 5 divisible values

I will customize a divisible function by the nearest 5 using the round() function.

Syntax:

round(number[, ndigits])

Parameters:

  • number: Number you want to round.
  • ndigits: The number of digits you want after the decimal point.

Return value from the round() function:

  • If there are no ndigits, the function returns the nearest integer.
  • If two arguments are provided, the function returns a number with ndigits decimal places after the decimal point.

Example:

# Use the round() function to round
def roundNearest5(x):
    return 5 * round(x / 5)
    
print('The nearest rounded number is divisible by 5:', roundNearest5(1))    
print('The nearest rounded number is divisible by 5:', roundNearest5(23)) 
print('The nearest rounded number is divisible by 5:', roundNearest5(12))

Output:

The nearest rounded number is divisible by 5: 0
The nearest rounded number is divisible by 5: 25
The nearest rounded number is divisible by 5: 10

In the above example, I instantiate a function that I named ’roundNearest5′ that returns the nearest divisible by 5 as follows: I use integer division (the / operator) as the round() parameter, and round() I don’t take any extra digits after the decimal point, so I always get an integer as a result. Multiply that result by 5 to get the nearest number that is divisible by 5.

You can also use the int() function in the ’roundNearest5′ function.

Example:

def roundNearest5(numParam, n = 5):
    return int(numParam) - int(numParam) % n
    
print('The nearest rounded number is divisible by 5:', roundNearest5(1))   
print('The nearest rounded number is divisible by 5:', roundNearest5(23))   
print('The nearest rounded number is divisible by 5:', roundNearest5(12))

Output:

The nearest rounded number is divisible by 5: 0
The nearest rounded number is divisible by 5: 20
The nearest rounded number is divisible by 5: 10

In the above example, I instantiate a function I named ‘myRound’ that returns the nearest divisible by 5: I use remainder division (% operator) as the int() function parameter. The int() function with that parameter returns an integer value. Multiply that result by 5 to get the nearest number that is divisible by 5.

Use the math.ceil() function

Syntax:

math.ceil( x )

Parameters:

  • x: A numeric expression.

The ceil() function returns the smallest integer that is not less than x.

Example:

  • Import math module to call ceil() function.
  • The ceil() function rounds the value from the function’s integer division. The result returned is an integer not less than the value of the division.
  • Multiply that result by 5 to get the nearest number that is divisible by 5.
import math

# Ues the math.ceil() to round
def roundNearest5(numParam, n = 5):
    return math.ceil(numParam / n) * n
    
print('The nearest rounded number is divisible by 5:', roundNearest5(1))
print('The nearest rounded number is divisible by 5:', roundNearest5(23))   
print('The nearest rounded number is divisible by 5:', roundNearest5(12))

Output:

The nearest rounded number is divisible by 5: 5
The nearest rounded number is divisible by 5: 25
The nearest rounded number is divisible by 5: 15

Summary

Some of the methods above have shown you how to round a number to the nearest 5 in Python. Hope you got an idea for this topic. Thank you for taking the time to read it.

Maybe you are interested:

Leave a Reply

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