Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python round up integer to next hundred

Seems that should have already been asked hundreds (pun are fun =) of times but i can only find function for rounding floats. How do I round up an integer, for example: 130 -> 200 ?

like image 949
userBG Avatar asked Jan 14 '12 22:01

userBG


People also ask

How do you round a number to the hundredth in Python?

Python has a built-in round() function that takes two numeric arguments, n and ndigits , and returns the number n rounded to ndigits . The ndigits argument defaults to zero, so leaving it out results in a number rounded to an integer.

How do you round up to the next 100?

When rounding to the nearest hundred, look at the TENS DIGIT of the number. If that digit is 0, 1, 2, 3, or 4, you will round down to the previous hundred. If that digit is 5, 6, 7, 8, or 9, you will round up to the next hundred.

How do you round up to the next integer?

Rounding to the Nearest Integer If the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.


2 Answers

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 
like image 187
Martin Geisler Avatar answered Oct 02 '22 12:10

Martin Geisler


This is a late answer, but there's a simple solution that combines the best aspects of the existing answers: the next multiple of 100 up from x is x - x % -100 (or if you prefer, x + (-x) % 100).

>>> x = 130 >>> x -= x % -100  # Round x up to next multiple of 100. >>> x 200 

This is fast and simple, gives correct results for any integer x (like John Machin's answer) and also gives reasonable-ish results (modulo the usual caveats about floating-point representation) if x is a float (like Martin Geisler's answer).

>>> x = 0.1 >>> x -= x % -100 >>> x 100.0 
like image 23
Mark Dickinson Avatar answered Oct 02 '22 12:10

Mark Dickinson