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')

Leave a Reply

Related Posts