【问题标题】:TypeError: unsupported operand type(s) for /: 'float' and 'decimal.Decimal'TypeError:不支持的操作数类型/:'float'和'decimal.Decimal'
【发布时间】:2022-01-03 10:53:57
【问题描述】:

让我们考虑一下我的观点.py

def ajax_add_other_order_item(request,id):
   client = request.user.client

   def _convert(from_currency, to_currency, price):
       custom_rate_obj = client.custom_rates.filter(currency=to_currency).first()
       if custom_rate_obj is None or custom_rate_obj.exchange_rate in (0, None):
          custom_rate_obj = ExchangeRates.objects.latest('created')
       return custom_rate_obj.convert(from_currency, to_currency, price)
   if request.method == 'POST':
      unit = request.POST.get('u_price') or 0
      if supplier.currency:
         currency = supplier.currency
      else:
         currency = request.POST.get('currency', 'GBP')
      purchase_price = _convert(currency, 'GBP', float(unit))
      try:
          exchange_price = float(unit)/purchase_price
      except ZeroDivisionError:
          exchange_price = 0
      if form_brought_in == 'CT':
            val = 2.5
      elif form_brought_in == 'BR' or form_brought_in == 'RB':
            val = 3.5
      else:
            val = 0.0
     trade_price = math.ceil(_convert(currency, client.currency, float(unit)) * val)

这是我的 models.py(其中定义了 custom_rate_obj.convert 函数)

class ExchangeRates(TimeStampedModel):
    date = models.DateField(unique=True)
    timestamp = models.BigIntegerField(blank=True, null=True)
    base_currency = models.CharField(max_length=10, default='GBP')
    data = JSONField()

    def get_currency_rate(self, currency):
        return Decimal(self.data[currency])

    def convert(self, from_currency, to_currency, value):
        from_rate = self.get_currency_rate(from_currency)
        to_rate = self.get_currency_rate(to_currency)


        return round((Decimal(value or 0) / from_rate) * to_rate, 6)

这是我的错误回溯

Traceback (most recent call last):
  File "/home/harika/lightdegree/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/harika/lightdegree/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/harika/lightdegree/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/harika/lightdegree/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/harika/lightdegree/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/harika/krishna test/dev-1.8/mcam/server/mcam/stock/views.py", line 4476, in ajax_add_other_order_item
    trade_price = math.ceil(_convert(currency, client.currency, float(unit)) * val)
TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'

这里我收到错误 TypeError: unsupported operand type(s) for /: 'float' and 'decimal.Decimal' 请帮助解决这个问题

【问题讨论】:

    标签: python python-3.x django django-models django-views


    【解决方案1】:

    您不能将floatDecimal 分开,因为小数的位数是固定的,而float 使用 IEEE-754 编码并且没有固定位数。

    您可以将数字转换为Decimal,然后进行相应的除法:

    from decimal import Decimal
    
    try:
        exchange_price = Decimal(unit)/purchase_price
    except ZeroDivisionError:
        exchange_price = Decimal(0)

    对于vals,您应该这样做:

    if form_brought_in == 'CT':
        val = Decimal('2.5')
    elif form_brought_in == 'BR' or form_brought_in == 'RB':
        val = Decimal('3.5')
    else:
        val = Decimal(0)

    【讨论】:

    • 在这里,当我将 float(unit) 更改为 Decimal(unit) 时,我在 trade_price 附近遇到另一个错误,我已经编辑了我的问题,请检查。我的错误是“trade_price = math.ceil(_convert(currency, client.currency, float(unit)) * val) TypeError: unsupported operand type(s) for *: 'decimal.Decimal' and 'float'”
    • @VEERABABU:请分享完整回溯。连同_convert的实现。
    • 现在请看我的回溯
    • @VEERABABU:能分享一下_convert的函数实现吗,另外val是从哪里来的?
    • 我的 _convert 函数已经写在视图中并添加了 val 也请现在检查
    【解决方案2】:
    from decimal import Decimal
    
    ...
    
    exchange_price = Decimal(unit)/purchase_price
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 2017-06-06
      • 2018-10-30
      • 2014-03-28
      • 2018-06-20
      • 2017-08-23
      • 1970-01-01
      相关资源
      最近更新 更多