PEP proposal for round(x,n) enhancement

Chris Barker chrishbarker at home.net
Wed Sep 12 18:47:56 EDT 2001


Christopher Smith wrote:
> ####
> from math import floor,log10
> def Round(x,n=0):
>         if n%1:
>                 d=int(10*n)
>                 return round(x,d-1-floor(log10(abs(x))))
>         else:
>                 return round(x,n)
> 
> print Round(1.23456,2) #returns 1.23
> print Round(1.23456,0.2) #returns 1.2

nifty, I didn't realise you could pass round a negative number of
digits... much cleaner than my version.

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list