Rounding floats with f-string

PythonPython 3.6

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

How about this

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

Docs for f-strings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionelsojaView Question on Stackoverflow
Solution 1 - PythonJBernardoView Answer on Stackoverflow