Calculate percentage saved between two numbers?

前端 未结 9 1539
自闭症患者
自闭症患者 2020-12-22 19:25

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

相关标签:
9条回答
  • 2020-12-22 19:38

    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 );
    }
    
    0 讨论(0)
  • 2020-12-22 19:39

    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%

    I hope that helps someone looking for the same kind of implementation.

    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.

    Thanks

    0 讨论(0)
  • 2020-12-22 19:44

    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;
    }
    
    0 讨论(0)
  • 2020-12-22 19:47
        function calculatePercentage($oldFigure, $newFigure)
    {
        $percentChange = (($oldFigure - $newFigure) / $oldFigure) * 100;
        return round(abs($percentChange));
    }
    
    0 讨论(0)
  • 2020-12-22 19:51
    ((list price - actual price) / (list price)) * 100%
    

    For example:

    ((25 - 10) / 25) * 100% = 60%
    
    0 讨论(0)
  • 2020-12-22 19:55

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

    0 讨论(0)
提交回复
热议问题