formatting a number as percentage

V

vsoler

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
 
T

TomF

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...
Grade is 87.000000%

or if you want to suppress those trailing zeroes:
Grade is 87%
 
M

Mark Dickinson

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):
'70.00%'

Or see TomF's response for how to use this with the str.format method.
 
G

Günther Dietrich

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

Did you try this:
70%



Best regards,

Günther
 
H

Hans Mulder

Günther Dietrich said:
Did you try this:

70%

That method will always round down; TomF's method will round to
the nearest whole number:
70%

Only the OP knows which one is more appropriate for his use case.

Hope this helps,

-- HansM
 
V

vsoler

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

Günther Dietrich

Hans Mulder said:
That method will always round down; TomF's method will round to
the nearest whole number:

70%

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



Best regards,

Günther
 
L

Lawrence D'Oliveiro

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)

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top