#Format numbers the right way in Python
Numbers can be formatted with the format() method. Here are some common use cases.
Round decimals
# Round decimals
print("{:.4f}".format(3.14159265359))
print("{:.2f}".format(3.14159265359))
print("{:.0f}".format(3.14159265359))3.1416
3.14
3Fixed width
# Fixed width, including decimals
print("{:06.2f}".format(12.7649))
print("{:06.1f}".format(12.7649))
print("{:06.0f}".format(12.7649))012.76
0012.8
000013Pad integers
# Padding integers
print("{:0>4d}".format(13))
print("{:->4d}".format(13))0013
--13Separators for large numbers
# Separators for large numbers
print("{:,.0f}".format(1079252848.85431))1,079,252,849Scientific notation
# Scientific notation
print("{:.4e}".format(1079252848.85431))1.0793e+09Percentages
# Percentage
print("{:.2%}".format(0.028547))2.85%Signed numbers
# Signed numbers
print("{:+.4f}".format(8.09421))
print("{:+.4f}".format(-8.09421))+8.0942
-8.0942