How to resolve TypeError: Unsupported operand type(s) for *: float and decimal.Decimal in Python

Unsupported operand type(s) for *: float and decimal.Decimal

To fix the TypeError: Unsupported operand type(s) for *: float and decimal.Decimal in Python. I will do the object conversion, use the Decimal module and check the object type. Post details below.

What causes the TypeError: Unsupported operand type(s) for *: float and decimal.Decimal?

The error happens when you multiply a float object by a decimal object.

Example: 

from decimal import Decimal

valueFloat = 1.2
decimalObj = Decimal('1.2')
print(valueFloat * decimalObj)

Output:

Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
TypeError: unsupported operand type(s) for *: 'float' and 'decimal.Decimal'

How to solve this error?

Converting float to decimal

To avoid the error, all you need to do is converting the float object to a decimal number.

Example:

  • Converting float to the decimal object using the Decimal() function.
  • Multiplying 2 decimal objects will not throw an error.
from decimal import Decimal

valueFloat = 1.2

# Float object converts to decimal.
valueFloat = Decimal(valueFloat)

decimalVar = Decimal(1.2)
print('Multiplication result:', valueFloat * decimalVar)

Output:

Multiplication result: 1.439999999999999893418589636

Or you can convert Decimal objects to floats using the float() function and multiply them together.

Example:

from decimal import Decimal

valueFloat = 1.2
decimalVar = Decimal(1.2)

# Decimal object converts to float.
decimalVar = float(decimalVar)
print('Multiplication result:', valueFloat * decimalVar)

Output:

Multiplication result: 1.44

Using the Decimal Module

Example:

  • Import the Decimal module.
  • Implement the decimal object using the Decimal() function from the Decimal module.
  • Perform rounding to 2 digits after the decimal point using the getcontext() function.
from decimal import Decimal, getcontext

firstDecimalFactor = Decimal(1.2)
secondDecimalFactor = Decimal(2.4)

# The getcontext() function rounds the math.
# Get 2 numbers after the decimal point.
getcontext().prec = 2
print('Multiplication result:', firstDecimalFactor * secondDecimalFactor)

Output:

Multiplication result: 2.9

When you enclose numbers in single quotes, the results are automatically rounded.

Example:

from decimal import Decimal

firstDecimalFactor = Decimal('1.2')
secondDecimalFactor = Decimal('2.4')
print('Multiplication result:', firstDecimalFactor * secondDecimalFactor)

Output:

Multiplication result: 2.88

Checking the decimal object

To check if an object is a decimal object, use the isinstance() function or the type() function.

Example:

from decimal import Decimal

firstDecimalFactor = Decimal('1.2')

# Use the isinstance() function to check.
print('Here is the decimal object:', isinstance(firstDecimalFactor, Decimal))

# Use the type() function to check.
print('What object is this?', type(firstDecimalFactor))

Output:

Here is the decimal object: True
What object is this? <class 'decimal.Decimal'>

Summary

The topic TypeError: Unsupported operand type(s) for *: float and decimal.Decimal done. You can use the Decimal module to solve this problem. It is pretty convenient. If you have any comments on the article, please leave a comment below.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *