Rounding Question

Remco Gerlich scarblac at pino.selwerd.nl
Wed Feb 21 12:04:23 EST 2001


Remco Gerlich <scarblac at pino.selwerd.nl> wrote in comp.lang.python:
> 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:

So I read 'round to the nearest 10' and Alex read it correctly...

> >    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
 
I'd go for 
rounded = number-(number%10)+10
now.

-- 
Remco Gerlich



More information about the Python-list mailing list