Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Percent Increase and Decrease

I'm likely over-thinking this, but I'm looking for something a bit more elegant than what I currently have.

I have this method called CalculatePercentageTrend, which takes in a long "previous" value and "current" value. As it currently stands :

public static string CalculatePercentageTrend(long previousValue, long currentValue)
{
    var trend = "0.00%";

    // calculate percentage increase
    if (previousValue < currentValue)
    {
        if (previousValue != 0)
        {
            var difference = previousValue - currentValue;
            var pctDecrease = (double)(difference / previousValue) * 100;
            trend = pctDecrease.ToString("P");
        }
        else
        {
            trend = currentValue.ToString("P");
        }
     }
    // calculate percentage decrease
    else if (currentValue < previousValue)
    {
        if (previousValue != 0)
        {
            var difference = currentValue - previousValue;
            var pctIncrease = (double)(difference / previousValue) * 100;
            trend = pctIncrease.ToString("P");
        }
        else
        {
            trend = currentValue.ToString("P");
        }
    }
    return trend;
}

This feels very repetitive, and has a few short comings. Negative trends aren't calculated properly, as they always result in a 0.00% change - what would be great is if I could get a negative percentage IF in fact the previous value is greater than the current value.

Also, I'm handling any possible 0's before dividing, but I'm wondering if there's a better approach to that as well.

My Question :

How can I get negative percentages to calculate correctly, and how can I improve this code overall?

like image 539
X3074861X Avatar asked Nov 25 '14 19:11

X3074861X


People also ask

How do I calculate a 20% increase in price?

Multiply the original price by 0.2 to find the amount of a 20 percent markup, or multiply it by 1.2 to find the total price (including markup). If you have the final price (including markup) and want to know what the original price was, divide by 1.2.

How do I calculate percentage difference?

What is the percentage difference? When the difference between two values is divided by the average of the same values, a percentage difference calculation has occurred. The formula for percentage difference looks like this: Percentage difference = Absolute difference / Average x 100.

What is the formula for decrease in percentage?

Percentage decrease formula can be obtained by simply dividing the decreased value by the original value and multiplying that with 100. Here, Decreased Value = Original Value – New Value.


1 Answers

You're way over thinking it.

double CalculateChange(long previous, long current)
{
    if (previous == 0)
        throw new InvalidOperationException();

    var change = current - previous;
    return (double)change / previous;
}

To display this data to a user in a friendly 'percentage' format, you can use:

string DoubleToPercentageString(double d)
{
    return "%" + (Math.Round(d, 2) * 100).ToString();
}
like image 98
Michael Avatar answered Sep 21 '22 20:09

Michael