Rounding Question

Mikael Olofsson mikael at isy.liu.se
Wed Feb 21 10:49:26 EST 2001


On 21-Feb-01 Remco Gerlich wrote:
 >  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

Actually, that should be

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

to get the behaviour Jacob asked for, provided that number is an integer.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    21-Feb-01
Time:    16:46:56

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------



More information about the Python-list mailing list