Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Format floating point number

  • :e
  • :E
  • :f
  • :F
  • :g
  • :G
  • :n
x = 412.345678901

print("{:e}".format(x))   #  exponent:     4.123457e+02
print("{:E}".format(x))   #  Exponent:     4.123457E+02
print("{:f}".format(x))   #  fixed point:  412.345679 (default precision is 6)
print("{:.2f}".format(x)) #  fixed point:  412.35 (set precision to 2)
print("{:F}".format(x))   #  same as f.    412.345679
print("{:g}".format(x))   #  generic:      412.346    (default precision is 6)
print("{:G}".format(x))   #  generic:      412.346
print("{:n}".format(x))   #  number:       412.346


print("{}".format(x))     # defaults to g  412.345678901