Round a decimal number

Erik Max Francis max at alcyone.com
Thu May 29 17:23:42 EDT 2003


Skip Montanaro wrote:

> Many people find the builtin round() function useful for this:
> 
>     >>> round(1.4463784692425, 2)
>     1.45

Note that's likely to cause some confusion:

>>> round(14.69372973975, 1)
14.699999999999999

Usually when one talks about rounding a floating point number, one wants
to do it for display.  So using the round function may not really be
what one wants; instead one should convert it for display:

>>> '%.1f' % 14.69372973975    
'14.7'

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ It's a sky-blue sky / Satellites are out tonight
\__/  Laurie Anderson




More information about the Python-list mailing list