How do I avoid division by zero error?

divide by zero error encountered in sql server stored procedure
divide by zero error encountered sql
error: division by zero postgresql
sql if error then null
divide by zero error encountered in stored procedure
sql if zero then 1
divide by zero error encountered in sql server query
divide by zero error encountered in c#

I have a code like this:

 add_filter( 'woocommerce_sale_flash2', 'lx_custom_onsale_label', 10, 2 );
  function lx_custom_onsale_label() {
    global $product;

    $percentage = round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) / $product->get_regular_price()  ) * 100 );
    $absolute = round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) ) );

    if ($product->get_regular_price()  > 100) {
    return '<span class="onsalex bg_primary headerfont">'. sprintf( __(' -%s', 'salex' ), $absolute . ',-' ).'</span>';
    } 

    else if ($product->get_regular_price()  < 1) {
    return '<span class="onsalessszzzx bg_primary headerfont">'. sprintf( __(' -%s', 'salex' ), $absolute . ',-' ).'</span>';
    }   

    else {
    return '<span class="onsalexzzz bg_primary headerfont">'. sprintf( __(' -%s', 'salex' ), $percentage . '%' ).'</span>';
    }
}

Everything work fine except when the divider is O, the notify will show:

Warning: Division by zero in D:\SERVER\InstantWP_4.3.1\iwpserver\htdocs\wordpress\wp-content\themes\MYTHEME\functions.php on line 553

Line 553 is this code:

    $percentage = round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) / $product->get_regular_price()  ) * 100 );

I don't understand how to avoid the warning with the condition zero on that code.

Really appreciate for the help.


Replace :

$percentage = round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) / $product->get_regular_price()  ) * 100 );

by :

$percentage = 0;
if ( $product->get_regular_price() > 0 ) 
    $percentage = round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) / $product->get_regular_price()  ) * 100 );

Strange answer I know, but if you don't divide by zero, there is no error.

Explain :

As @domdom point out "Don't divide by zero" which is here a good answer, and a good practice since divide by zero is not "legal" in mathematics.

Methods to avoid the SQL divide by zero error, Let's explore it in the next section. Method 1: SQL NULLIF Function. We use NULLIF function to avoid divide by zero error message. The syntax of  Prevent divide by zero errors (#DIV/0!) with modifying formulas The formula of =D2/(C2-B2) will return the #DIV!/0 error if Cell C2 is equal to Cell B2. So we can estimate this formula with the IFERROR function to avoid the #DIV/0! error. You can do as follows:


Just check if $product->get_regular_price() is greater than/ not equal to zero (in case negative values are possible):

if ($product->get_regular_price() != 0){
  // Do stuff
} else {
  // Do something with the zero
}

With only positive numbers:

if ($product->get_regular_price() > 0){
  // Do stuff
} else {
  // Do something with the zero
}

Five SQL Tips from Chris Fehily |, You can use the function NULLIF to avoid division by zero. NULLIF Any number divided by NULL gives NULL, and no error is generated. Software like Excel simply gives you an error when you try to divide by zero. In Excel, when you divide a number by zero, you get the #DIV/0! error. You can avoid this by telling Excel to skip the calculation if your denominator is a zero.


if($product->get_regular_price()> 0)
{
//do your work
}

Just check if the price is zero before the division.

How to avoid the “divide by zero” error in SQL?, To avoid "Division by zero" error you should use the following code: SELECT Case. WHEN divisor=0 then null. ELSE dividend/divisor. End … Just like solution #1, solution #2 avoid divide by zero error from appearing. The Formula =IFERROR(D3/(B3-C3),"No Sales") The Result. The result is the same as solution # 1, the divide by zero error disappears in cells E5 and E9.


Replace this line

 $percentage = round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) / $product->get_regular_price()  ) * 100 );

with

 $percentage = $product->get_regular_price() != 0 ? round( ( ( $product->get_regular_price()  - $product->get_sale_price() ) / $product->get_regular_price()  ) * 100 ) : 0;

The question has been answered already, this is just another approach using the tenary operator

Using NULLIF() To Prevent Divide-By-Zero Errors In SQL, Error Executing Database Query. [Macromedia] [SQLServer JDBC Driver] [​SQLServer] Divide by zero error encountered. To prevent this sort of  Method 2: Using CASE statement to avoid divide by zero error We can use a CASE statement in SQL to return values based on specific conditions. Look at the following query. It does the following task with the Case statement.


try it. something like this is simple but useful..

$var = @($val1 / $val2);

Divide by zero error encountered., There are three ways to avoid the "Division by zero encountered" error in your SELECT statement and these are as follows: CASE statement. NULLIF/ISNULL functions. SET ARITHABORT OFF and SET ANSI_WARNINGS OFF. You cannot divide by NULL or 0 – it is mathematical non-sense. For example. 1 / 0 = mathematical non-sense. To fix the issue you need to find the formula that’s causing the issue and add a check at the start to say ‘if value is 0 then return 0 else do calculation’. For example. IF {#field_1} = 0 OR {#field_2} = 0 THEN 0 ELSE {#field_1


Avoid Divide By Zero Errors In SQL - SQL Best Practices, This will cause a divide by 0 error that will most likely break your system, ETL, or adhoc query.Duration: 3:43 Posted: Feb 2, 2019 When creating formulas, that use a division operator, it is always advisable to consider the possibility that the demoninator field value may be zero: To get around this problem, the formula was modified to:


Fix & Prevent Excel DIV/0! Errors • Productivity Portfolio, error message. AKA the dreaded “divide by zero error”. Article Contents. What Does #DIV/0  How to Show a Zero Value instead of #DIV/0! Create a column for your formula. (e.g. Column E Conv Cost) Click the next cell down in that column. (e.g. E2) Click the Formulas tab on the Excel ribbon. Click the Insert Function button on the far left. In the Insert Function dialog , select IF . Click


Avoid a Division by Zero Error with the NULLIF and NULLIFZERO , Both come in handy when you want to avoid a “Division by zero” error. Example: dbadmin=> SELECT 1 / 0 "This errors!"; ERROR 3117: Division  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.