Show profit or loss in negative percentage

Considering the following data:

$despesas = 2000;
$receitas = 100;
$lucroprejuizo = ($receitas - $despesas) / $receitas)*100;

I have a result of: -1900%

But if any of the variables income or expenses is zero the calculation of the wrong because it will be dividing by zero. Is there any way to calculate? For example if we consider that I had 0,00 of income and 300,00 of expenses he will have -100% of profit.

Author: Marcos A. Silva, 2016-03-06

1 answers

The case of DESP expense = 0 you would have to filter with an "if" condition, because it represents an infinite profit (revenue without any expense). To eliminate division by zero when the recipe is equal to zero, the formula can be modified to:

100 * ($receitas / $despesas) - 100

In this formula, if income=300 and expense=250, the result is +20 (20% profit). If revenue=0 and expense is non-zero, the result is always -100%.

 1
Author: epx, 2016-03-07 02:04:40