[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

formatting a number as percentage

vsoler

2/21/2010 5:54:00 PM

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...

Can you help?

Thank you
8 Answers

TomF

2/21/2010 6:11:00 PM

0

On 2010-02-21 09:53:45 -0800, vsoler <vicente.soler@gmail.com> said:
> 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...

>>> 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%

Mark Dickinson

2/21/2010 6:15:00 PM

0

On Feb 21, 5:53 pm, vsoler <vicente.so...@gmail.com> wrote:
> 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...

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

vsoler

2/21/2010 6:17:00 PM

0

On Feb 21, 7:11 pm, TomF <tomf.sess...@gmail.com> wrote:
> On 2010-02-21 09:53:45 -0800, vsoler <vicente.so...@gmail.com> said:
>
> > 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...
> >>> 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%

Excellent, works perfect!!!

GüntherDietrichgd_usenet

2/21/2010 6:18:00 PM

0

vsoler <vicente.soler@gmail.com> wrote:

>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

Hans Mulder

2/22/2010 7:33:00 PM

0

Günther Dietrich wrote:
> vsoler <vicente.soler@gmail.com> wrote:
>
>> 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%

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

vsoler

2/23/2010 7:36:00 PM

0

On Feb 22, 8:32 pm, Hans Mulder <han...@xs4all.nl> wrote:
> Günther Dietrich wrote:
> > vsoler <vicente.so...@gmail.com> wrote:
>
> >> 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%
>
> 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

Great!!!

Thank you

GüntherDietrichgd_usenet

2/23/2010 8:10:00 PM

0

Hans Mulder <hansmu@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

Lawrence D'Oliveiro

2/24/2010 2:04:00 AM

0

In message <6819f2f8-7a9e-4ea4-a936-c4e00394bd30@g28g2000yqh.googlegroups.com>, vsoler wrote:

> 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)

:)