*:'float'和'Decimal'不受支持的操作数类型 [英] unsupported operand type(s) for *: 'float' and 'Decimal'
本文介绍了*:'float'和'Decimal'不受支持的操作数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在学习类函数之类的东西,所以我决定创建一个简单的函数,该函数应该给我税额。
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.
class VAT_calculator:
"""
A set of methods for VAT calculations.
"""
def __init__(self, amount=None):
self.amount = amount
self.VAT = decimal.Decimal('0.095')
def initialize(self):
self.amount = 0
def total_with_VAT(self):
"""
Returns amount with VAT added.
"""
if not self.amount:
msg = u"Cannot add VAT if no amount is passed!'"
raise ValidationError(msg)
return (self.amount * self.VAT).quantize(self.amount, rounding=decimal.ROUND_UP)
我的问题是出现以下错误:
My issue is I'm getting the following error:
不受支持的操作数类型)*:'float'和'Decimal'**
unsupported operand type(s) for *: 'float' and 'Decimal'**
我不明白为什么这不起作用!
I don't see why this should not work!
推荐答案
好像 self.VAT
是十进制十进制
类型和 self.amount
是 float
,这是您无法做到的
It seems like self.VAT
is of decimal.Decimal
type and self.amount
is a float
, thing that you can't do.
尝试 decimal.Decimal(self.amount)* self.VAT
代替。
这篇关于*:'float'和'Decimal'不受支持的操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文