Is it possible to set the precision globally for an "f" string?

Viewed 111

The following Python 3.8 code:

a = [(1/3, 1/6), 1/9]
print(f"{a}")

prints:

[(0.3333333333333333, 0.16666666666666666), 0.1111111111111111]

I would like to print only three decimal digits. I tried:

print(f"{a:.3f}")

but got an error, probably because a is not a float.

I can write a recursive function that goes through a and rounds each element of it, but it is cumbersome.

Is there a simpler way to set a global "float printing precision", so that the above function would print something like:

[(0.333, 0.167), 0.111]
0 Answers
Related