Two ways output percentage Python3

Two ways output percentage Python

Note: The test in python3 environment.

Embodiment 1: Parameters used directly format: 2% :.} {
{:} .2%: display to one decimal place

Display 2 decimal places:
>>> Print ( 'Percent: {: .2%}' the format (42/50).)
Percent: 84.00%
. 1
2
does not show decimals: {} :. 0%, i.e., the 2 to 0:
>>> Print ( 'Percent: {: .0%}' the format (42/50).)
Percent: 84%
. 1
2
mode 2: formatted to float, and then processed to form%: {: .2f}%
difference from embodiment 1 is:

(1) multiplied by 100 needs to 42/50. 
2% (2) In the embodiment 1, outside% {}, {} in the way inside.

Display 2 decimal places:
>>> Print ( 'Percent: {:% .2f}' the format (42 is / 50 * 100).)
Percent: 84.00%
. 1
2
after a decimal point:
>>> Print ( 'Percent : {:}% .1f 'the format (42 is / 50 * 100)).
Percent: 84.0%
. 1
2
shows only bit integer:
>>> Print (' Percent: {:} .0f.% 'the format (42/50 100 *))
Percent: 84%
1
2
described
{} means a parameter corresponding to the format (), the corresponding default order parameter numbers start with 0, 0} corresponding to {format () of the first parameter, {1 } corresponding to the second parameter. E.g:

Default order:
>>> Print ( 'percent1: {:} .2%, percent2: {: .1%}'. The format (42/50, 42/100))
percent1: 84.00%, percent2: 42.0%
. 1
2
Specifies the sequence: 
{1: 0.1%} corresponds to the second parameter; {0: .1%} corresponds to the first parameter.
Print >>> ( 'percent2: {. 1: .1%}, percent1: {0: .1%}'. The format (42/50, 42/100))
percent2: 42.0%, percent1: 84.0%

Guess you like

Origin blog.csdn.net/naonaoxiansheng/article/details/93484947