Rounding Question

Remco Gerlich scarblac at pino.selwerd.nl
Wed Feb 21 09:43:39 EST 2001


Jacob Kaplan-Moss <jacobkm at cats.ucsc.edu> wrote in comp.lang.python:
> Hello All --
> 
> So I've got a number between 40 and 130 that I want to round up to the 
> nearest 10.  That is:
>    
>    40 --> 40
>    41 --> 50
>    42 --> 50
>    ...
>    49 --> 50
>    50 --> 50
>    51 --> 60

Rounding like this is the same as adding 5 to the number and then rounding
down. Rounding down is substracting the remainder if you were to divide by
10, for which we use the % operator in Python.

rounded = (number+5)-(number+5)%10

-- 
Remco Gerlich



More information about the Python-list mailing list