 # How To Round A Number To The Nearest 100 In Python Rounding is a pretty important skill in Python. In this article, I will show you how to round a number to the nearest 100 in Python. I would use ways like customizing a function using Python’s built-in functions and using two functions, round up and round down, in the math module.

## Round a number to the nearest 100 in Python

### Customize a function that returns the nearest 100 divisible values

I will customize a divisible function by the nearest 100 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 myRound(x):
return round(x / 100) * 100

print("The nearest rounded number is divisible by 100:", myRound(255))
print("The nearest rounded number is divisible by 100:", myRound(311))
print("The nearest rounded number is divisible by 100:", myRound(51))

Output:

The nearest rounded number is divisible by 100: 200
The nearest rounded number is divisible by 100: 300
The nearest rounded number is divisible by 100: 100

In the above example, I instantiate a function that I named ‘myRound’ that returns the nearest divisible by 100 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 100 to get the nearest number that is divisible by 100.

The second way to use the round() function to round a number to the nearest 100 is setting the ndigits parameter to -2, which has the effect of rounding to the nearest multiple of 100.

Example:

def myRound(value):
return round(value, -2)

print("The nearest rounded number is divisible by 100:", myRound(243))
print("The nearest rounded number is divisible by 100:", myRound(311))
print("The nearest rounded number is divisible by 100:", myRound(134))

Output:

The nearest rounded number is divisible by 100: 200
The nearest rounded number is divisible by 100: 300
The nearest rounded number is divisible by 100: 100

### Use the math.ceil() and math.floor() functions

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 100 to get the nearest number that is divisible by 100.
import math

# Use the math.ceil() to round up nearest 100
def myRound(number, n = 100):
return math.ceil(number / 100) * 100

print("Number rounded up to the nearest divisible by 100:", myRound(243))
print("Number rounded up to the nearest divisible by 100:", myRound(311))
print("Number rounded up to the nearest divisible by 100:", myRound(134))

Output:

Number rounded up to the nearest divisible by 100: 300
Number rounded up to the nearest divisible by 100: 400
Number rounded up to the nearest divisible by 100: 200

To round down some of the best way using the math.floor() function.

Syntax:

math.floor(x)

Parameters:

• x: The number you need to round down.

The math.floor() function will round down a number to the nearest integer.

Example:

import math

# Use the math.ceil() to round down nearest 100
def myRound(number, n = 100):
return math.floor(number / 100) * 100

print('Number rounded up to the nearest divisible by 100:', myRound(243))
print('Number rounded up to the nearest divisible by 100:', myRound(311))
print('Number rounded up to the nearest divisible by 100:', myRound(134))

Output:

Number rounded down to the nearest divisible by 100: 200
Number rounded down to the nearest divisible by 100: 300
Number rounded đown to the nearest divisible by 100: 100

## Summary

So the article is over. I hope you understand how to round a number to the nearest 100 in Python. You can use the round() function and set the 2nd parameter to – 2 for short. Thank you for taking the time to read this article.

Maybe you are interested: