Multiplication With Decimals in Python

You can work with Decimals in Two ways. Float method and Import Decimal package both ways you can use.

Multiplication with Decimals

Can use float type to solve decimal problems.

body_fat_percentage = 0.10
weight = 200
fat_total = body_fat_percentage * weight
print(f"I weight 200lbs, and {fat_total}lbs of that is fat")

    I weight 200lbs, and 20.0lbs of that is fat

Can also use Decimal Library to set precision and deal with repeating decimal

from decimal import (Decimal, getcontext)

getcontext().prec = 2
Decimal(1)/Decimal(3)

    Decimal('0.33')

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.

Start Discussion

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.