Python round up to nearest 100

Rounding is typically done on floating point numbers, and here there are three basic functions you should know: round (rounds to the nearest integer), math.floor (always rounds down), and math.ceil (always rounds up).

You ask about integers and rounding up to hundreds, but we can still use math.ceil as long as your numbers smaller than 253. To use math.ceil, we just divide by 100 first, round up, and multiply with 100 afterwards:

>>> import math
>>> def roundup(x):
...     return int(math.ceil(x / 100.0)) * 100
... 
>>> roundup(100)
100
>>> roundup(101)
200

Dividing by 100 first and multiply with 100 afterwards "shifts" two decimal places to the right and left so that math.ceil works on the hundreds. You could use 10**n instead of 100 if you want to round to tens (n = 1), thousands (n = 3), etc.

An alternative way to do this is to avoid floating point numbers (they have limited precision) and instead use integers only. Integers have arbitrary precision in Python, so this lets you round numbers of any size. The rule for rounding is simple: find the remainder after division with 100, and add 100 minus this remainder if it's non-zero:

>>> def roundup(x):
...     return x if x % 100 == 0 else x + 100 - x % 100

This works for numbers of any size:

>>> roundup(100)
100
>>> roundup(130)
200
>>> roundup(1234567891234567891)
1234567891234567900L

I did a mini-benchmark of the two solutions:

$ python -m timeit -s 'import math' -s 'x = 130' 'int(math.ceil(x/100.0)) * 100'
1000000 loops, best of 3: 0.364 usec per loop
$ python -m timeit -s 'x = 130' 'x if x % 100 == 0 else x + 100 - x % 100'
10000000 loops, best of 3: 0.162 usec per loop

The pure integer solution is faster by a factor of two compared to the math.ceil solution.

Thomas proposed an integer based solution that is identical to the one I have above, except that it uses a trick by multiplying Boolean values. It is interesting to see that there is no speed advantage of writing the code this way:

$ python -m timeit -s 'x = 130' 'x + 100*(x%100>0) - x%100'
10000000 loops, best of 3: 0.167 usec per loop

As a final remark, let me also note, that if you had wanted to round 101–149 to 100 and round 150–199 to 200, e.g., round to the nearest hundred, then the built-in round function can do that for you:

>>> int(round(130, -2))
100
>>> int(round(170, -2))
200

Python – Round Number to Nearest 10

To round number to nearest 10, use round() function. We can divide the value by 10, round the result to zero precision, and multiply with 10 again. Or you can pass a negative value for precision. The negative denotes that rounding happens to the left of the decimal point.

In this tutorial, we will write Python programs to round a number to its nearest 10 or 100.

For example, if the number is 3652, then its nearest number to 10 is 3650. And the nearest 100 is 3700.

Example 1 – Round Number to Nearest 10 using round()

In this example, we will read a number from user, and round the value to its nearest 10 using round() function.

Python Program

number = int(input('Enter a number :'))
rounded = round(number/10)*10
print('Rounded Number :', rounded)

Output

Enter a number :3652
Rounded Number : 3650

Or you can also provide a negative number as the second argument for round() function, to round to those number of digits before decimal point.

Python Program

number = int(input('Enter a number :'))
rounded = round(number, -1)
print('Rounded Number :', rounded)

Output

Enter a number :3652
Rounded Number : 3650

Example 2 – Round Number to Nearest 100 using round()

In this example, we will round the number to its nearest 100 using round() function.

Python Program

number = int(input('Enter a number :'))
rounded = round(number/100)*100
print('Rounded Number :', rounded)

Output

Enter a number :3652
Rounded Number : 3700

Or you can also provide a negative number as the second argument for round() function, to round to those number of digits before decimal point. To round to nearest 100, we need to provide -2 as second argument to round().

Python Program

number = int(input('Enter a number :'))
rounded = round(number, -2)
print('Rounded Number :', rounded)

Output

Enter a number :3652
Rounded Number : 3700

Conclusion

Concluding this Python Tutorial, we learned how to use round() function to round a number to nearest 10 or 100. You can use the same process to find the nearest number to any digit before the decimal point.

How do you round up to 100?

Rounding to the nearest 100 To round a number to the nearest 100, look at the tens digit. If the tens digit is 5 or more, round up. If the tens digit is 4 or less, round down. The tens digit in 3281 is 8.

How do I get Python to round up?

To implement the “rounding up” strategy in Python, we'll use the ceil() function from the math module. The ceil() function gets its name from the term “ceiling,” which is used in mathematics to describe the nearest integer that is greater than or equal to a given number.

Does Python round up or down?

The round() function rounds a number to the nearest whole number. The math. ceil() method rounds a number up to the nearest whole number while the math. floor() method rounds a number down to the nearest whole number.

How do you round to the nearest 50 in Python?

Python round() Function.
The round() returns a number rounded to ndigits precision after the decimal point..
If ndigits is not specified, the number is rounded to the nearest integer..
round(number,ndigits).
You can specify precision after the decimal point by using ndigits parameter..
ndigits can be negative as well..