Hướng dẫn python print percentage - tỷ lệ phần trăm in python

Đây là mã của tôi:

Nội phân chính

  • %S có nghĩa là gì trong Python?
  • Làm cách nào để chuyển đổi một chuỗi thành phần trăm trong Python?
  • %S có nghĩa là gì trong Python?
  • Làm cách nào để chuyển đổi một chuỗi thành phần trăm trong Python?
print str(float(1/3))+'%'

và nó cho thấy:

0.0%

Nhưng tôi muốn nhận

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

Tôi có thể làm gì?

Hướng dẫn python print percentage - tỷ lệ phần trăm in python

Martineau

Huy hiệu vàng 115K2525 gold badges160 silver badges282 bronze badges

hỏi ngày 15 tháng 3 năm 2011 lúc 2:10Mar 15, 2011 at 2:10

3

>>> 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%
4 hỗ trợ một tỷ lệ phần trăm điểm chính xác:

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

Nếu bạn không muốn phân chia số nguyên, bạn có thể nhập bộ phận của Python3 từ

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

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

Maxymoo

33.5K9 Huy hiệu vàng89 Huy hiệu bạc115 Huy hiệu đồng9 gold badges89 silver badges115 bronze badges

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

Mikumikumiku

175K46 Huy hiệu vàng302 Huy hiệu bạc 307 Huy hiệu đồng46 gold badges302 silver badges307 bronze badges

7

Có một cách thuận tiện hơn 'Tùy chọn định dạng phần trăm cho phương thức định dạng

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

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

Đã trả lời ngày 20 tháng 5 năm 2014 lúc 16:00May 20, 2014 at 16:00

5

Chỉ vì sự hoàn chỉnh, vì tôi nhận thấy không ai đề xuất cách tiếp cận đơn giản này:

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

Details:

  • >>> 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%
    
    7 là viết tắt của "In phao với 0 thập phân", vì vậy
    >>> 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%
    
    8 sẽ in
    >>> 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%
    
    9
  • >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    0 in một nghĩa đen
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    1. Sạch hơn một chút so với
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    2 ban đầu của bạn
  • >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    3 thay vì
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    4 chăm sóc việc ép buộc bộ phận để nổi, do đó không còn
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    5

Đã trả lời ngày 15 tháng 8 năm 2013 lúc 10:52Aug 15, 2013 at 10:52

MestrelionmestrelionMestreLion

11.9k4 Huy hiệu vàng62 Huy hiệu bạc55 Huy hiệu Đồng4 gold badges62 silver badges55 bronze badges

6

Chỉ để thêm giải pháp chuỗi Python 3 F

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

Đã trả lời ngày 24 tháng 9 năm 2019 lúc 21:48Sep 24, 2019 at 21:48

Menrfamenrfamenrfa

1.23711 huy hiệu bạc13 huy hiệu đồng11 silver badges13 bronze badges

4

Bạn đang chia số nguyên sau đó chuyển đổi thành float. Chia cho phao thay thế.

Như một phần thưởng, hãy sử dụng các phương thức định dạng chuỗi tuyệt vời được mô tả ở đây: http://docs.python.org/l Library

Để chỉ định một phần trăm chuyển đổi và độ chính xác.

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

Một viên ngọc tuyệt vời!

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

Mikumiku

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

175K46 Huy hiệu vàng302 Huy hiệu bạc 307 Huy hiệu đồng

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

3

Mikumiku

ratio = round(1/3, 2) 
print(f"{ratio} %")

output: 0.33 %

175K46 Huy hiệu vàng302 Huy hiệu bạc 307 Huy hiệu đồngAug 19, 2021 at 8:16

Có một cách thuận tiện hơn 'Tùy chọn định dạng phần trăm cho phương thức định dạng

>>> 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%
6:DGKang

Đã trả lời ngày 20 tháng 5 năm 2014 lúc 16:0012 bronze badges

1

Chỉ vì sự hoàn chỉnh, vì tôi nhận thấy không ai đề xuất cách tiếp cận đơn giản này:

0.0%
0

>>> 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%
7 là viết tắt của "In phao với 0 thập phân", vì vậy
>>> 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%
8 sẽ in
>>> 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%
9Jul 11 at 20:59

%S có nghĩa là gì trong Python?

>>> '{:.1%}'.format(1/3.0)
'33.3%'
0 in một nghĩa đen
>>> '{:.1%}'.format(1/3.0)
'33.3%'
1. Sạch hơn một chút so với
>>> '{:.1%}'.format(1/3.0)
'33.3%'
2 ban đầu của bạnused to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.

Làm cách nào để chuyển đổi một chuỗi thành phần trăm trong Python?

>>> '{:.1%}'.format(1/3.0)
'33.3%'
3 thay vì
>>> '{:.1%}'.format(1/3.0)
'33.3%'
4 chăm sóc việc ép buộc bộ phận để nổi, do đó không còn
>>> '{:.1%}'.format(1/3.0)
'33.3%'
5use python's string format syntax '{:. 2%}'. format to add the '%' sign back.

Đây là mã của tôi:

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

và nó cho thấy:

0.0%

Nhưng tôi muốn nhận

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

Tôi có thể làm gì?

Martineau

Huy hiệu vàng 115K2525 gold badges160 silver badges282 bronze badges

hỏi ngày 15 tháng 3 năm 2011 lúc 2:10Mar 15, 2011 at 2:10

3

>>> 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%
4 hỗ trợ một tỷ lệ phần trăm điểm chính xác:

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

Nếu bạn không muốn phân chia số nguyên, bạn có thể nhập bộ phận của Python3 từ

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

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

Maxymoo

33.5K9 Huy hiệu vàng89 Huy hiệu bạc115 Huy hiệu đồng9 gold badges89 silver badges115 bronze badges

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

Mikumikumiku

175K46 Huy hiệu vàng302 Huy hiệu bạc 307 Huy hiệu đồng46 gold badges302 silver badges307 bronze badges

7

Có một cách thuận tiện hơn 'Tùy chọn định dạng phần trăm cho phương thức định dạng

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

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

Đã trả lời ngày 20 tháng 5 năm 2014 lúc 16:00May 20, 2014 at 16:00

5

Chỉ vì sự hoàn chỉnh, vì tôi nhận thấy không ai đề xuất cách tiếp cận đơn giản này:

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

Details:

  • >>> 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%
    
    7 là viết tắt của "In phao với 0 thập phân", vì vậy
    >>> 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%
    
    8 sẽ in
    >>> 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%
    
    9
  • >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    0 in một nghĩa đen
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    1. Sạch hơn một chút so với
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    2 ban đầu của bạn
  • >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    3 thay vì
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    4 chăm sóc việc ép buộc bộ phận để nổi, do đó không còn
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    5

Đã trả lời ngày 15 tháng 8 năm 2013 lúc 10:52Aug 15, 2013 at 10:52

MestrelionmestrelionMestreLion

11.9k4 Huy hiệu vàng62 Huy hiệu bạc55 Huy hiệu Đồng4 gold badges62 silver badges55 bronze badges

6

Chỉ để thêm giải pháp chuỗi Python 3 F

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

Đã trả lời ngày 24 tháng 9 năm 2019 lúc 21:48Sep 24, 2019 at 21:48

Menrfamenrfamenrfa

1.23711 huy hiệu bạc13 huy hiệu đồng11 silver badges13 bronze badges

4

Bạn đang chia số nguyên sau đó chuyển đổi thành float. Chia cho phao thay thế.

Như một phần thưởng, hãy sử dụng các phương thức định dạng chuỗi tuyệt vời được mô tả ở đây: http://docs.python.org/l Library

Để chỉ định một phần trăm chuyển đổi và độ chính xác.

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

Một viên ngọc tuyệt vời!

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

Mikumiku

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

175K46 Huy hiệu vàng302 Huy hiệu bạc 307 Huy hiệu đồng

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

3

Mikumiku

ratio = round(1/3, 2) 
print(f"{ratio} %")

output: 0.33 %

175K46 Huy hiệu vàng302 Huy hiệu bạc 307 Huy hiệu đồngAug 19, 2021 at 8:16

Có một cách thuận tiện hơn 'Tùy chọn định dạng phần trăm cho phương thức định dạng

>>> 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%
6:DGKang

Đã trả lời ngày 20 tháng 5 năm 2014 lúc 16:0012 bronze badges

1

Chỉ vì sự hoàn chỉnh, vì tôi nhận thấy không ai đề xuất cách tiếp cận đơn giản này:

0.0%
0

>>> 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%
7 là viết tắt của "In phao với 0 thập phân", vì vậy
>>> 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%
8 sẽ in
>>> 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%
9Jul 11 at 20:59

%S có nghĩa là gì trong Python?

>>> '{:.1%}'.format(1/3.0)
'33.3%'
0 in một nghĩa đen
>>> '{:.1%}'.format(1/3.0)
'33.3%'
1. Sạch hơn một chút so với
>>> '{:.1%}'.format(1/3.0)
'33.3%'
2 ban đầu của bạnused to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.

Làm cách nào để chuyển đổi một chuỗi thành phần trăm trong Python?

Để chuyển đổi nó trở lại thành phần trăm chuỗi, chúng ta sẽ cần sử dụng cú pháp định dạng chuỗi của Python '{: .2%}'. Định dạng để thêm dấu hiệu '%'.use python's string format syntax '{:. 2%}'. format to add the '%' sign back.

Đây là mã của tôi:

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

và nó cho thấy:

0.0%

Nhưng tôi muốn nhận

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

Tôi có thể làm gì?

Martineau

Huy hiệu vàng 115K2525 gold badges160 silver badges283 bronze badges

hỏi ngày 15 tháng 3 năm 2011 lúc 2:10Mar 15, 2011 at 2:10

3

>>> 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%
4 hỗ trợ một tỷ lệ phần trăm điểm chính xác:

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

Nếu bạn không muốn phân chia số nguyên, bạn có thể nhập bộ phận của Python3 từ

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

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

Maxymoo

33.5K9 Huy hiệu vàng89 Huy hiệu bạc115 Huy hiệu đồng9 gold badges89 silver badges115 bronze badges

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

Mikumikumiku

175K46 Huy hiệu vàng303 Huy hiệu bạc 307 Huy hiệu đồng46 gold badges303 silver badges307 bronze badges

7

Có một cách thuận tiện hơn 'Tùy chọn định dạng phần trăm cho phương thức định dạng

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

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

Đã trả lời ngày 20 tháng 5 năm 2014 lúc 16:00May 20, 2014 at 16:00

5

Chỉ vì sự hoàn chỉnh, vì tôi nhận thấy không ai đề xuất cách tiếp cận đơn giản này:

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

Details:

  • >>> 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%
    
    7 là viết tắt của "In phao với 0 thập phân", vì vậy
    >>> 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%
    
    8 sẽ in
    >>> 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%
    
    9
  • >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    0 in một nghĩa đen
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    1. Sạch hơn một chút so với
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    2 ban đầu của bạn
  • >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    3 thay vì
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    4 chăm sóc việc ép buộc bộ phận để nổi, do đó không còn
    >>> '{:.1%}'.format(1/3.0)
    '33.3%'
    
    5

Đã trả lời ngày 15 tháng 8 năm 2013 lúc 10:52Aug 15, 2013 at 10:52

MestrelionmestrelionMestreLion

11.9k4 Huy hiệu vàng62 Huy hiệu bạc55 Huy hiệu Đồng4 gold badges62 silver badges55 bronze badges

6

Chỉ để thêm giải pháp chuỗi Python 3 F

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

Đã trả lời ngày 24 tháng 9 năm 2019 lúc 21:48Sep 24, 2019 at 21:48

Menrfamenrfamenrfa

1.23711 huy hiệu bạc13 huy hiệu đồng11 silver badges13 bronze badges

4

Bạn đang chia số nguyên sau đó chuyển đổi thành float. Chia cho phao thay thế.

Như một phần thưởng, hãy sử dụng các phương thức định dạng chuỗi tuyệt vời được mô tả ở đây: http://docs.python.org/l Library

Để chỉ định một phần trăm chuyển đổi và độ chính xác.

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

Một viên ngọc tuyệt vời!

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

Mikumiku

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

175K46 Huy hiệu vàng303 Huy hiệu bạc 307 Huy hiệu đồng

Đã trả lời ngày 15 tháng 3 năm 2011 lúc 2:16Mar 15, 2011 at 2:16

3

Mikumiku

ratio = round(1/3, 2) 
print(f"{ratio} %")

output: 0.33 %

175K46 Huy hiệu vàng303 Huy hiệu bạc 307 Huy hiệu đồngAug 19, 2021 at 8:16

Có một cách thuận tiện hơn 'Tùy chọn định dạng phần trăm cho phương thức định dạng

>>> 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%
6:DGKang

Đã trả lời ngày 20 tháng 5 năm 2014 lúc 16:0012 bronze badges

1

Chỉ vì sự hoàn chỉnh, vì tôi nhận thấy không ai đề xuất cách tiếp cận đơn giản này:

0.0%
0

>>> 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%
7 là viết tắt của "In phao với 0 thập phân", vì vậy
>>> 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%
8 sẽ in
>>> 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%
9Jul 11 at 20:59