Rounding Question
Alex Martelli
aleaxit at yahoo.com
Wed Feb 21 17:05:36 CET 2001
"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
news:slrn997ktk.h0.scarblac at pino.selwerd.nl...
> 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
This will work if you use +9 in each case rather than +5 (note that he
doesn't really want rounding -- he wants 41 to 'round' to 50, for ex),
and it's Yet Another Way To Do It similar but not identical to the dozen
or so I explained in my previous post on this thread...:-)
Alex
More information about the Python-list
mailing list