round() function
Michael Rudolf
spamfresser at ch3ka.de
Thu Feb 25 10:51:40 EST 2010
Am 25.02.2010 16:39, schrieb Tracubik:
> hi all, i've this sample code:
>
>>>> 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.
>
> How can I solve this?
>
> Thanks in advance
> Nico
This has nothing to do with round().
round() returns a float, and float(4.50) is 4.5.
You can *print* it as 4.50 using format strings, but the float will
always be 4.5
>>> n = 4.499
>>> x=round(n,2)
>>> x
4.5
>>> print "%.2f" % x
4.50
>>> s="%.2f" % round(n,2)
>>> s
'4.50'
>>>
More information about the Python-list
mailing list