Rounding floats with f-string


Using %-formatting, I can specify the number of decimal cases in a string:

x = 3.14159265
print('pi = %0.2f' %x)

This would give me:

pi = 3.14

Is there any way of doing this using f-strings in Python 3.6?

Asked By: elsoja



How about this

x = 3.14159265
print(f'pi = {x:.2f}')

Docs for f-strings

Answered By: JBernardo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.