83

I have two numbers, the first, is the original price, the second, is the discounted price.

I need to work out what percentage a user saves if they purchase at the second price.

example
25, 10 = 60%  
365, 165 = 55%

What I dont know is the formula to calculate this.

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • 3
    @ShreevatsaR - would you like me to rephrase the question to say "PHP formula to Calculate percentage saved between two numbers" Then people can write `$diff = (($listPrice - $actualPrice) / ($listPrice)) * 100;` either way, you cant really write a program without using a formula, hence, this question is programming related. – Hailwood Apr 27 '11 at 04:25
  • I think it would be improved if you made it explicit that it is programming related. As written, it's purely a math question. – r12 Apr 27 '11 at 13:01
  • I did actually go to ask it on math.stackexchange.com but that seems a bit out of place, seeing as how I couldn't even find the tags 'formula', or 'percentage'! – Hailwood Apr 28 '11 at 21:14

11 Answers11

211

I know this is fairly old but I figured this was as good as any to put this. I found a post from yahoo with a good explanation:

Let's say you have two numbers, 40 and 30.  

  30/40*100 = 75.
  So 30 is 75% of 40.  

  40/30*100 = 133. 
  So 40 is 133% of 30. 

The percentage increase from 30 to 40 is:  
  (40-30)/30 * 100 = 33%  

The percentage decrease from 40 to 30 is:
  (40-30)/40 * 100 = 25%. 

These calculations hold true whatever your two numbers.

Original Post

cking24343
  • 3,173
  • 1
  • 21
  • 23
38
((list price - actual price) / (list price)) * 100%

For example:

((25 - 10) / 25) * 100% = 60%
Andy White
  • 86,444
  • 48
  • 176
  • 211
  • 1
    Technically, you would multiply the 100, then print the % sign. The value "100%" isn't always the same as the value for "100". – r12 Apr 27 '11 at 03:43
  • 2
    Indeed... the percent calculation always seemed funny to me – Andy White Apr 27 '11 at 03:44
  • 4
    "%" means "per cent" means "1/100", and "100%" means 100/100 = 1. So multiplying by "100%" is the same as multiplying by 1: it leaves the value unchanged. Thus 0.6 = 0.6 * 100% = 60%, similarly 1/2 = 1/2 * 100% = 50%, etc. – ShreevatsaR Apr 27 '11 at 03:51
17

I see that this is a very old question, but this is how I calculate the percentage difference between 2 numbers:

(1 - (oldNumber / newNumber)) * 100

So, the percentage difference from 30 to 40 is:

(1 - (30/40)) * 100 = +25% (meaning, increase by 25%)

The percentage difference from 40 to 30 is:

(1 - (40/30)) * 100 = -33.33% (meaning, decrease by 33%)

In php, I use a function like this:

function calculatePercentage($oldFigure, $newFigure) {
        if (($oldFigure != 0) && ($newFigure != 0)) {
            $percentChange = (1 - $oldFigure / $newFigure) * 100;
        }
        else {
            $percentChange = null;
        }
        return $percentChange;
}
Neel
  • 9,352
  • 23
  • 87
  • 128
11

The formula would be (original - discounted)/original. i.e. (365-165)/365 = 0.5479...

Random832
  • 37,415
  • 3
  • 44
  • 63
6
    function calculatePercentage($oldFigure, $newFigure)
{
    $percentChange = (($oldFigure - $newFigure) / $oldFigure) * 100;
    return round(abs($percentChange));
}
nikmauro
  • 683
  • 7
  • 13
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Dec 02 '16 at 09:04
5

100% - discounted price / full price

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

If total no is: 200 and getting 50 number

then take percentage of 50 in 200 is:

(50/200)*100 = 25%

Zahid Gani
  • 169
  • 6
1

I have done the same percentage calculator for one of my app where we need to show the percentage saved if you choose a "Yearly Plan" over the "Monthly Plan". It helps you to save a specific amount of money in the given period. I have used it for the subscriptions.

Monthly paid for a year - 2028 Yearly paid one time - 1699

1699 is a 16.22% decrease of 2028.

Formula: Percentage of decrease = |2028 - 1699|/2028 = 329/2028 = 0.1622 = 16.22%

Code:

func calculatePercentage(monthly: Double, yearly: Double) -> Double {
    let totalMonthlyInYear = monthly * 12
    let result = ((totalMonthlyInYear-yearly)/totalMonthlyInYear)*100
    print("percentage is -",result)
    return result.rounded(toPlaces: 0)
}

Usage:

 let savingsPercentage = self.calculatePercentage(monthly: Double( monthlyProduct.price), yearly: Double(annualProduct.price))
 self.btnPlanDiscount.setTitle("Save \(Int(savingsPercentage))%",for: .normal)

The extension usage for rounding up the percentage over the Double:

extension Double {
/// Rounds the double to decimal places value
func rounded(toPlaces places:Int) -> Double {
    let divisor = pow(10.0, Double(places))
    return (self * divisor).rounded() / divisor
   }
}

I have attached the image for understanding the same:

Percentage Calc - Subscription

Mike Q
  • 6,716
  • 5
  • 55
  • 62
Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
0

This is function with inverted option

It will return:

  • 'change' - string that you can use for css class in your template
  • 'result' - plain result
  • 'formatted' - formatted result

function getPercentageChange( $oldNumber , $newNumber , $format = true , $invert = false ){

    $value      = $newNumber - $oldNumber;

    $change     = '';
    $sign       = '';

    $result     = 0.00;

    if ( $invert ) {
         if ( $value > 0 ) {
        //  going UP
            $change             = 'up';
            $sign               = '+';
            if ( $oldNumber > 0 ) {
                $result         = ($newNumber / $oldNumber) * 100;
            } else {
                $result     = 100.00;
            }

        }elseif ( $value < 0 ) {        
        //  going DOWN
            $change             = 'down';
            //$value                = abs($value);
            $result             = ($oldNumber / $newNumber) * 100;
            $result             = abs($result);
            $sign               = '-';

        }else {
        //  no changes
        }

    }else{

        if ( $newNumber > $oldNumber ) {

            //  increase
            $change             = 'up';

            if ( $oldNumber > 0 ) {

                $result = ( ( $newNumber / $oldNumber ) - 1 )* 100;

            }else{
                $result = 100.00;
            }

            $sign               = '+';

        }elseif ( $oldNumber > $newNumber ) {

            //  decrease
            $change             = 'down';

            if ( $oldNumber > 0 ) {

                $result = ( ( $newNumber / $oldNumber ) - 1 )* 100;

            } else {
                $result = 100.00;
            }

            $sign               = '-';

        }else{

            //  no change

        }

        $result = abs($result);

    }

    $result_formatted       = number_format($result, 2);

    if ( $invert ) {
        if ( $change == 'up' ) {
            $change = 'down';
        }elseif ( $change == 'down' ) {
            $change = 'up';
        }else{
            //
        }

        if ( $sign == '+' ) {
            $sign = '-';
        }elseif ( $sign == '-' ) {
            $sign = '+';
        }else{
            //
        }
    }
    if ( $format ) {
        $formatted          = '<span class="going '.$change.'">'.$sign.''.$result_formatted.' %</span>';
    } else{
        $formatted          = $result_formatted;
    }

    return array( 'change' => $change , 'result' => $result , 'formatted' => $formatted );
}
Sayonara
  • 267
  • 1
  • 10
0

I think this covers this formula sufficiently,

((curr value - base value) / (curr value)) * 100%

Basically we just (in programming):

  • perform the calculation if both numbers are not 0.
  • If curr value is 0 then we return -100 % difference from the base,
  • if both are 0 then return 0 (we can't divide by 0)

Powershell example:

Strip any non numeric from vars and perform calculation

Function Get-PercentageSaved {
    #((curr value - base value) / (curr value)) * 100%
    param(
        [Parameter(Mandatory = $false)][string]$CurrVal = $null,
        [Parameter(Mandatory = $false)][string]$BaseVal = $null
    )
    $Result = $null
    Try {

        $CurrVal = [float]($CurrVal -replace '[^0-9.]', '')
        $BaseVal = [float]($BaseVal -replace '[^0-9.]', '')

        if (-Not($null -eq $CurrVal) -And (-Not($null -eq $BaseVal))) {
            if ($CurrVal -eq 0) {
                If ($BaseVal -eq 0) {
                    $Result = 0
                } Else {
                    $Result = -100
                }
            }
            else {
                $Result = [math]::Round([float]((($CurrVal - $BaseVal) / $CurrVal) * 100),2)
            }
        }
    }
    Catch {}
    Return [float]$Result
}
Mike Q
  • 6,716
  • 5
  • 55
  • 62
0

I'm used this logic, maybe it will be useful to someone. In example if you try to calculate percentage change number of followers per day. Today you have 54 followers, yesterday it was only 4, it's 1.250% change in one day. Inverse situation, today you have only 4 followers, yesterday it was 54, it's -1.250% change. For this situation you must change divider number.

  1. example ((54-4)/4)*100 = 1250
  2. example ((4-54)/4)*100 = -1250
public function getPercentage(int $recent, int $previous): int
{
    $minVal = min($recent, $previous);

    if (!$minVal)
            return 0;
    
    return (($recent - $previous) / $minVal) * 100;
}
Slawe
  • 71
  • 4