【问题标题】:Calculate percentage saved between two numbers?计算两个数字之间节省的百分比?
【发布时间】:2011-08-13 12:46:28
【问题描述】:

我有两个数字,第一个是原价,第二个是折扣价。

如果用户以第二个价格购买,我需要计算出他们节省了多少百分比。

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

我不知道计算这个的公式。

【问题讨论】:

  • @ShreevatsaR - 你想让我把问题改写成“计算两个数字之间节省的百分比的 PHP 公式”然后人们可以写 $diff = (($listPrice - $actualPrice) / ($listPrice)) * 100; 任何一种方式,你不能真正编写程序而不使用一个公式,因此,这个问题与编程有关。
  • 我认为如果你明确表示它与编程相关,它会得到改进。正如所写,这纯粹是一道数学题。
  • 我确实去 math.stackexchange.com 上询问过,但这似乎有点不合适,因为我什至找不到标签“公式”或“百分比”!

标签: math formula


【解决方案1】:

100% - 折扣价/全价

【讨论】:

  • @Klas:这是我们想要的。
【解决方案2】:

公式为(original - discounted)/original。即 (365-165)/365 = 0.5479...

【讨论】:

  • (9.999.999 - 369.990) / 9.999.999 = 0,9630009963000996300099630009963?但应该超过 2700% 对吧?
【解决方案3】:
((list price - actual price) / (list price)) * 100%

例如:

((25 - 10) / 25) * 100% = 60%

【讨论】:

  • 从技术上讲,您将乘以 100,然后打印 % 符号。 “100%”的值并不总是与“100”的值相同。
  • 确实......百分比计算对我来说总是很有趣
  • "%" 表示 "percent" 表示 "1/100",而 "100%" 表示 100/100 = 1。因此乘以 "100%" 与乘以 1 相同:它使值保持不变。因此 0.6 = 0.6 * 100% = 60%,类似地 1/2 = 1/2 * 100% = 50%,等等。
【解决方案4】:

我知道这已经相当老了,但我认为这和任何东西一样好。我找到了一个来自 yahoo 的帖子,其中有一个很好的解释:

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

【讨论】:

  • 说得好兄弟
【解决方案5】:

我看到这是一个非常古老的问题,但这是我计算两个数字之间百分比差异的方法:

(1 - (oldNumber / newNumber)) * 100

所以,从 30 到 40 的百分比差异是:

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

从 40 到 30 的百分比差异是:

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

在 php 中,我使用这样的函数:

function calculatePercentage($oldFigure, $newFigure) {
        if (($oldFigure != 0) && ($newFigure != 0)) {
            $percentChange = (1 - $oldFigure / $newFigure) * 100;
        }
        else {
            $percentChange = null;
        }
        return $percentChange;
}

【讨论】:

    【解决方案6】:
        function calculatePercentage($oldFigure, $newFigure)
    {
        $percentChange = (($oldFigure - $newFigure) / $oldFigure) * 100;
        return round(abs($percentChange));
    }
    

    【讨论】:

    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
    【解决方案7】:

    这是带有反转选项的函数

    它会返回:

    • 'change' - 可用于模板中的 css 类的字符串
    • 'result' - 普通结果
    • 'formatted' - 格式化结果

    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 );
    }
    

    【讨论】:

      【解决方案8】:

      我为我的一个应用做了同样的百分比计算器,我们需要显示如果您选择“年计划”而不是“月计划”,节省的百分比。它可以帮助您在给定期间节省特定金额的钱。我已经用它来订阅了。

      每月支付一年 - 2028 一年付一次 - 1699

      1699 比 2028 年减少 16.22%

      公式: 减少百分比 = |2028 - 1699|/2028 = 329/2028 = 0.1622 = 16.22%

      我希望这对寻找相同类型实现的人有所帮助。

      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)
      }
      

      用法:

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

      在 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
         }
      }
      

      我附上了图片以便理解。

      谢谢

      【讨论】:

        【解决方案9】:

        如果总数是:200 并获得 50 个号码

        那么在 200 中取 50 的百分比是:

        (50/200)*100 = 25%

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-28
          • 2016-02-27
          相关资源
          最近更新 更多