文章内容

2023/5/30 15:30:59,作 者: 黄兵

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

在进行汇率计算的时候,出现了如下错误:

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

出现错误的原因:

无法对 Decimalfloat 的两种不同数据类型进行乘法运算。

具体错误代码如下:

try:
USD_CNY_exchange_rate = CurrencyExchangeUtil().get_usd_to_cny()
except TimeoutError:
USD_CNY_exchange_rate = 7
sum_price = str(price * duration * 1 * USD_CNY_exchange_rate)

解决方案:

修改 USD_CNY_exchange_rate 的数据类型为:Decimal

try:
USD_CNY_exchange_rate = CurrencyExchangeUtil().get_usd_to_cny()
except TimeoutError:
USD_CNY_exchange_rate = 7
sum_price = str(price * duration * 1 * Decimal(USD_CNY_exchange_rate))

这个问题就解决了。


参考资料:

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


其它相关推荐:

1、AttributeError: 'str' object has no attribute 'items'

2、ValueError: fe80::204:61ff:fe9d:f156/11 has host bits set

3、AttributeError: 'sqlalchemy.cimmutabledict.immutabledict' object has no attribute 'setdefault'

4、error: invalid command 'bdist_wheel'

5、Object of type Decimal is not JSON serializable


黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

分享到:

发表评论

评论列表