round() function

Tim Chase python.list at tim.thechases.com
Thu Feb 25 10:45:16 EST 2010


Tracubik wrote:
>>>> n = 4.499
>>>> str(round(n,2))
> '4.5'
> 
> that's right, but what i want is '4.50' to be displayed instead of '4.5'.
> Off course i know that 4.5 = 4.50, still i'ld like to have 4.50.

Use string formatting:

   >>> "%0.2f" % round(4.499, 2)
   '4.50'

-tkc






More information about the Python-list mailing list