Hi.
I have put a simple division together but when the denominator is 0 then i get the
float division by zero error
Here in my code:
i found some examples and tried them but nothing works.
Any help would be appreciated
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
Hi.
I have put a simple division together but when the denominator is 0 then i get the
float division by zero error
Here in my code:
i found some examples and tried them but nothing works.
Any help would be appreciated
Hi,
It seems like you want to avoid the "float division by zero" error when
record.price_subtotal is zero. You can add a check to handle this
situation. Here's an example of how you can modify your code to handle
division by zero:
for record in self: |
for record in self: |
Hope it helps
Thank you, Both work
Hello Pete Charalampopoulos,
To resolve this error you just need to add a condition to check price_subtotal as follow and if the price_subtotal is zero then x_studio_gross_margin should also be zero.
for record in self:
record['x_studio_gross_margin'] = (record.x_studio_gross_profit / record.price_subtotal) if record.price_subtotal else 0
I hope this will help you.
Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari
To handle the "float division by zero" error in Python, you can use a try-except block to catch the `ZeroDivisionError` exception. This way, you can manage the situation when the denominator is zero without having your program crash.
Here's an example of how you might modify your code to handle this:
```python
try:
# Your division code here
# Example: result = numerator / denominator
except ZeroDivisionError:
# Handle the division by zero error
# or
# result = None
```
In this example, replace the comment `# Your division code here` with your actual division operation. If the denominator in your division is zero, the `ZeroDivisionError` will be raised, and the code within the `except` block will execute. This allows you to handle the error gracefully, such as by printing an error message or setting the result to a default value like `None`.
Hi Charles
i have added your recommendation on the bottom of my code
and i get the following error:
except ZeroDivisionError:
IndentationError: unexpected indent
here is the code:
for record in self:
record['x_studio_gross_margin'] = record.x_studio_gross_profit / record.price_subtotal
except ZeroDivisionError:
result = None
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up