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

前端 未结 2 683
遇见更好的自我
遇见更好的自我 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.

    0 讨论(0)
  • 2021-02-05 00:03

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

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

    0 讨论(0)
提交回复
热议问题