print format(.7,'%%')
.7.format('%%')
but neither works. I don't know what the syntax is...
Can you help?
Thank you
>>> print "Grade is {0:%}".format(.87)
Grade is 87.000000%
or if you want to suppress those trailing zeroes:
>>> print "Grade is {0:.0%}".format(.87)
Grade is 87%
Assuming that you're using Python 2.6 (or Python 3.x):
>>> format(.7, '%')
'70.000000%'
>>> format(.7, '.2%')
'70.00%'
Or see TomF's response for how to use this with the str.format method.
--
Mark
Excellent, works perfect!!!
>I'm trying to print .7 as 70%
>I've tried:
>
>print format(.7,'%%')
>.7.format('%%')
>
>but neither works. I don't know what the syntax is...
Did you try this:
>>> print('%d%%' % (0.7 * 100))
70%
Best regards,
Günther
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%
Only the OP knows which one is more appropriate for his use case.
Hope this helps,
-- HansM
>> 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
> I'm trying to print .7 as 70%
Just to be perverse:
(lambda x : (lambda s : s[:s.index(".")] + s[s.index(".") + 1:] + "%")("%.2f" % x).lstrip("0"))(.7)
:)