Skip to Content
Menu
This question has been flagged
3 Replies
3399 Views

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

 

Avatar
Discard
Best Answer

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:
if record.price_subtotal != 0:
    record['x_studio_gross_margin'] = record.x_studio_gross_profit / record.price_subtotal
else:
    # Handle the division by zero case, for example, set x_studio_gross_margin to a default value
    record['x_studio_gross_margin'] = 0.0  # You can choose any default value here


This modification checks if record.price_subtotal is not equal to zero before performing the division. If it's zero, it sets x_studio_gross_margin to a default value (in this case, 0.0). You can adjust the default value according to your requirements.

or you can try


for record in self:
try:
    record['x_studio_gross_margin'] = record.x_studio_gross_profit / record.price_subtotal
except ZeroDivisionError:
    # Handle the division by zero case, for example, set x_studio_gross_margin to a default value
    record['x_studio_gross_margin'] = 0.0  # You can choose any default value here


Hope it helps

Avatar
Discard
Author

Thank you, Both work

Best Answer

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

Avatar
Discard
Best Answer

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`.

Avatar
Discard
Author

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