Modified: July 03 2024


7/3/2024

Taken from this article.

Often in projects, we want to format our large float types as smaller percentages.

There are three main ways to do this:

  1. Rounding and Float/String Types:

round() takes two parameters: a value and a number to round too. Then, we can use a little complicated string statement.

>>> number = 0.8839133112
>>> n2 = round(number, 4)
>>> print(n2)
0.886
>>> print(str(n2 * 100) + '%')
  1. Format()

Alternatively, if that seems a bit complicated, we can just format() instead.

>>> print(format(n2, ".2%"))
  1. F-string

We can also use formatted string literals, which to me seems like the best way.

print(f'{n2:.2%}')