formatting a number as percentage

Günther Dietrich gd_usenet at spamfence.net
Tue Feb 23 15:09:38 EST 2010


Hans Mulder <hansmu at xs4all.nl> wrote:

>> Did you try this:
>> 
>>>>> print('%d%%' % (0.7 * 100))
>> 70%
>
>That method will always round down; TomF's method will round to
>the nearest whole number:
>
> >>> print "%d%%" % (0.698 * 100)
>69%
> >>> print "{0:.0%}".format(.698)
>70%

It was intended as a hint to this way of formatting. He could also try:

>>> print('%.0f%%' % (0.698 * 100))
70%



Best regards,

Günther



More information about the Python-list mailing list