How to format to n decimal places in Python
Fields in format strings can be nested:
>>> print(f"{math.pi:.{n}f}")
3.14159
For pre-3.6 versions, you can use .format()
print('{:.{}}'.format(math.pi, n)))
Fields in format strings can be nested:
>>> print(f"{math.pi:.{n}f}")
3.14159
For pre-3.6 versions, you can use .format()
print('{:.{}}'.format(math.pi, n)))