'unsupported operand type(s) for -: 'decimal.Decimal' and 'float' in Python 3
I am getting this issue unsupported operand type(s) for -: 'decimal.Decimal' and 'float'
Here is my models.py
def _convert(self,from_currency, to_currency, price):
custom_rate_obj = self.quote.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)
def get_margin(self):
if self.cost_price:
cost_price = self.cost_price
unit_price = self._convert(self.currency, self.cost_currency, self.unit_price)
if unit_price:
return ((unit_price - float(cost_price))/unit_price) * 100
How can i solve my issue
Solution 1:[1]
Change the unit_price to float as well.
return ((float(unit_price) - float(cost_price))/unit_price) * 100
And always use typing in python 3 t avoid such errors.
def _convert(self,from_currency: str, to_currency : str, price: float) => float:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rama |
