## 问题：如何在python中打印百分比值？

``print str(float(1/3))+'%'``

``0.0%``

this is my code:

``````print str(float(1/3))+'%'
``````

and it shows:

``````0.0%
``````

but I want to get `33%`

What can I do?

## 回答 0

`format`支持百分比浮点精度类型

``````>>> print "{0:.0%}".format(1./3)
33%``````

``````>>> from __future__ import division
>>> 1 / 3
0.3333333333333333

# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format(1/3 * 100)
33%

# Or even shorter using the format mini language:
>>> print "{:.0%}".format(1/3)
33%``````

`format` supports a percentage floating point precision type:

``````>>> print "{0:.0%}".format(1./3)
33%
``````

If you don’t want integer division, you can import Python3’s division from `__future__`:

``````>>> from __future__ import division
>>> 1 / 3
0.3333333333333333

# The above 33% example would could now be written without the explicit
# float conversion:
>>> print "{0:.0f}%".format(1/3 * 100)
33%

# Or even shorter using the format mini language:
>>> print "{:.0%}".format(1/3)
33%
``````

## 回答 1

``````>>> '{:.1%}'.format(1/3.0)
'33.3%'``````

There is a way more convenient ‘percent’-formatting option for the `.format()` format method:

``````>>> '{:.1%}'.format(1/3.0)
'33.3%'
``````

## 回答 2

``````>>> print("%.0f%%" % (100 * 1.0/3))
33%``````

• `%.0f`代表“ 打印带有0个小数位的浮点数 ”，因此`%.2f`将打印`33.33`
• `%%`打印文字`%`。比原来的要干净一点`+'%'`
• `1.0`而不是`1`强迫分部浮动，所以不再`0.0`

Just for the sake of completeness, since I noticed no one suggested this simple approach:

``````>>> print("%.0f%%" % (100 * 1.0/3))
33%
``````

Details:

• `%.0f` stands for “print a float with 0 decimal places“, so `%.2f` would print `33.33`
• `%%` prints a literal `%`. A bit cleaner than your original `+'%'`
• `1.0` instead of `1` takes care of coercing the division to float, so no more `0.0`

## 回答 3

``````>>> float(1) / float(3)
[Out] 0.33333333333333331

>>> 1.0/3.0
[Out] 0.33333333333333331

>>> '{0:.0%}'.format(1.0/3.0) # use string formatting to specify precision
[Out] '33%'

>>> '{percent:.2%}'.format(percent=1.0/3.0)
[Out] '33.33%'``````

You are dividing integers then converting to float. Divide by floats instead.

As a bonus, use the awesome string formatting methods described here: http://docs.python.org/library/string.html#format-specification-mini-language

To specify a percent conversion and precision.

``````>>> float(1) / float(3)
[Out] 0.33333333333333331

>>> 1.0/3.0
[Out] 0.33333333333333331

>>> '{0:.0%}'.format(1.0/3.0) # use string formatting to specify precision
[Out] '33%'

>>> '{percent:.2%}'.format(percent=1.0/3.0)
[Out] '33.33%'
``````

A great gem!

## 回答 4

``````prob = 1.0/3.0
print(f"{prob:.0%}")``````

Just to add Python 3 f-string solution

``````prob = 1.0/3.0
print(f"{prob:.0%}")
``````

## 回答 5

``print str(int(1.0/3.0*100))+'%'``

`.0`表示他们的花车和`int()`事后再发他们的整数。

Then you’d want to do this instead:

``````print str(int(1.0/3.0*100))+'%'
``````

The `.0` denotes them as floats and `int()` rounds them to integers afterwards again.