I would like to know what is the best solution to find the percentage change of a double compared to another one. For example, I have a variable called 'current', I want to compare it to a variable called 'previous'. I need this for statistic purposes so that I can analyze the growth or the loss of value. I wrote my own algorithm, but it's very basic and it includes 2 if(s), which makes it too programming language dependent. Ideally I would like to find a one-line calculation.
double current = 10;
double previous = 20;
double result = 0.0;
// growth
if (current > previous)
{
result = (100 / current) * previous;
}
// loss
if (current < previous)
{
result = (100 / previous) * current;
}
Do you have any advice?