Python Format Float (Decimal Points) Value or None

Aug 17, 2019
v1 = 0.00000028v2 = Noneprint(f"v1={v1}, v2={v2}")# v1=2.8e-07, v2=None

To print with decimal points without exponential notation

print(f"v1={v1:.8f}, v2={v2}")# v1=0.00000028, v2=None

Sadly, TypeError: unsupported format string passed to NoneType.__format__ is raised when the value is None

print(f"v1={v1:.8f}, v2={v2:.8f}")

To handle formating of float value which could be None as well.

def f(v):    if v is None:        return v    return f"{v:.08f}"print(f"v1={f(v1)}, v2={f(v2)}")# v1=0.00000028, v2=None

NOTE: Format float without trailing zeros.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.