[Django]-Unsupported operand type(s) for *: 'float' and 'Decimal'

112👍

It seems like self.VAT is of decimal.Decimal type and self.amount is a float, a thing that you can’t do.

Try decimal.Decimal(self.amount) * self.VAT instead.

👤aldeb

21👍

Your issue is, as the error says, that you’re trying to multiply a Decimal by a float

The simplest solution is to rewrite any reference to amount declaring it as a Decimal object:

self.amount = decimal.Decimal(float(amount))

and in initialize:

self.amount = decimal.Decimal('0.0')

Another option would be to declare Decimals in your final line:

return (decimal.Decimal(float(self.amount)) * self.VAT).quantize(decimal.Decimal(float(self.amount)), rounding=decimal.ROUND_UP)

…but that seems messier.

👤jymbob

2👍

I was getting this error on this line of code:

row_value = float(row['hours']) * 0.2

and changed it to this:

row_value = float(row['hours']) * float(0.2)

which worked fine.

Leave a comment