Python Print/Show Decimal Value With Precision Decimal Points

Feb 25, 2021
Avoid scrientific notation and long decimals
def decimal_to_str(v, exp=None, prec=None):    if exp:        # exp = Decimal('.00000001')        prec = abs(exp.as_tuple().exponent)    if not prec:        prec = 8    return f"{v:.{prec}f}"
import decimald = Decimal('1.44E+4')print(str(d))             # 1.44E+4print(decimal_to_str(d))  # 14400.00000000d = decimal.Decimal(1.33)print(str(d))             # 1.3300000000000000710542735760100185871124267578125print(decimal_to_str(d))  # 1.33000000

❤️ 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.