unsupported operand type(s) for *: 'float' and 'Decimal'

前端 未结 2 682
遇见更好的自我
遇见更好的自我 2021-02-04 23:30

I\'m just playing around learning classes functions etc, So I decided to create a simple function what should give me tax amount.

this is my code so far.



        
2条回答
  •  死守一世寂寞
    2021-02-05 00:00

    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.

提交回复
热议问题