Skip to content

Latest commit

 

History

History
25 lines (16 loc) · 563 Bytes

File metadata and controls

25 lines (16 loc) · 563 Bytes

F-String Number Formatting

Force Decimal Places

To force a number to show specific number of decimal places in a python f-string, use this formatting code:

>>> print(f"{45.2:.02f}")
45.20

Force Leading Zeros

To force leading zeros when printing an integer, user this code:

>>> print(f"{3:02d}")
03

References

Python f-string cheat sheet provides a many different number formatting examples.

fstring.help provides more example including non-numeric formatting.