Home > Back-end >  f-string with percent and fixed decimals?
f-string with percent and fixed decimals?

Time:11-24

I know that I can to the following. But is it possible to combine them to give me a percent with fixed decimal?

>>> print(f'{0.123:%}')
12.300000%
>>> print(f'{0.123:.2f}')
0.12

But what I want is this output:

12.30%

CodePudding user response:

You can specify the number of decimal places before %:

>>> f'{0.123:.2%}' 
'12.30%'
  • Related