Calculate increment/decrement percentage from two values

1.7k Views Asked by At

Hi all I got a very stupid question which I could not figure out how to solve it.

So basically I am trying to calculate how many percentage of increment/decrement for the expense and income of current from past month.

So let's say I got these data:

Current Month Expense: 10454
Current Month Income:   2233
Last Month Expense:    15
Last Month Income:     1500

To calculate the income:

if(thisMonthtotalIncome < lastMonthtotalIncome){
        incomePercentage = Math.round(thisMonthtotalIncome/lastMonthtotalIncome * 100);
    }else if(thisMonthtotalIncome > lastMonthtotalIncome){
        incomePercentage = Math.round((thisMonthtotalIncome/lastMonthtotalIncome));
    }

To calculate the expense:

if(thisMonthtotalExpense < lastMonthtotalExpense){
        expensePercentage = Math.round(thisMonthtotalExpense/lastMonthtotalExpense * 100);
    }else if(thisMonthtotalExpense > lastMonthtotalExpense){
        expensePercentage = Math.round((thisMonthtotalExpense/lastMonthtotalExpense));
    }

I realized the percentage was somewhat wrong because from the data above, I get the expense increases by 697%. Is there any way or correct way to limit it to be in the range of 0% - 100%?

Also, I think that the formula is wrong as well cause I am so lousy at Maths.

Any suggestions? Thanks in advance!

2

There are 2 best solutions below

10
On BEST ANSWER

Use these:

incomePercentage = Math.round(thisMonthtotalIncome/lastMonthtotalIncome * 100);
expensePercentage = Math.round(thisMonthtotalExpense/lastMonthtotalExpense * 100);

Always think percentage as fractions. Close to 1. Only multiply by 100 for fanciness:

Note that you never have to limit them.... never.

%DI=2233/1500=1.48666 (148.66%)

%DE=10454/15=696.9333 (69693.33%)

These are huge, and very bad because you only had \$15 at past month and now you have \$10.454. What did you do with those \$10 bucks you saved the past year?... Very bad. Numbers do not lie.

EDIT:

IF you are obtaining several NaN because you obtained a zero income|expense during the last period, this means, the metric itself is very "spiky" for your process. In this case, you can always make a smoother reference:

HistoricIncome =0.2*HistoricIncome +0.8*lastMonthtotalIncome;
HistoricExpense =0.2*HistoricExpense +0.8*lastMonthtotalExpense;    
incomePercentage = Math.round(thisMonthtotalIncome/HistoricIncome * 100);
expensePercentage = Math.round(thisMonthtotalExpense/HistoricExpense * 100);

which calculates an Historic Income|Expense average, by taking the 80% of the actual last month value, but smoothed by the previous calculated historic value. This is enough smooth for not having a close to zero indicator. This is an "exponential average". Several averaging techniques exist for obtaining an historic moving value to use as reference.

4
On

Your algorithm is wrong. At one place you are multiplying with 100, while in the other case you aren't. Multiplying with 100 gives percentage (Per-cent, or per hundred), while not multiplying with 100 just gives you the fraction.

Let me explain how it works:

Say, you have a quantity Q and its initial and final values are Qin and Qfi respectively. Now, to check whether there is an increase or a decrease, you take the ratio, Q(final) / Q(initial), that is, Qfi/Qin.

If this ratio is > 1, then Qfi > Qin, so there is an increase. While, if it is < 1, there is a decrease. To calculate, how much there is an increase or decrease, you subtract this ratio from 1.

So say, Qfi/Qin=0.6. This means, Qfi = 0.6 * Qin. So, Qfi is 0.6 times the Qin. Multiplying 0.6 by 100, we get 60%, which means, it is just 60% of the initial quantity (or we can say it is 40% less). The notion of it being 40% less is captured by the following:

1 - 0.6 = 0.4

This, says that the final quantity (Qfi which is exactly 0.6 * Qin) is 0.4 * Qin less than the value Qin. And multiplying 0.4 with 100, you get 40%, which means Qfi is 40% less than Qin.

Now, coming to your problem, make sure that you multiply by 100 in both the conditions, so that you get a percentage output in each case.

if(thisMonthtotalExpense < lastMonthtotalExpense){
        expensePercentage = 
        Math.round( (thisMonthtotalExpense/lastMonthtotalExpense) * 100);
}else if(thisMonthtotalExpense > lastMonthtotalExpense){
        expensePercentage = 
        Math.round( (thisMonthtotalExpense/lastMonthtotalExpense) * 100);
}

In both the cases, you'll get the expense percentage, with respect to the last month expense. And since, now your formula takes into account the 100 factor, you will get 69700% instead of 697%.

Hope this helps.