Math 计算两个数字之间的保存百分比?

Math 计算两个数字之间的保存百分比?,math,formula,Math,Formula,我有两个数字,第一个是原价,第二个是折扣价 我需要计算出用户以第二种价格购买时节省的百分比 example 25, 10 = 60% 365, 165 = 55% 我不知道的是计算这个的公式。100%折扣价/全价这个公式应该是(原始-折扣)/original。i、 e.(365-165)/365=0.5479 ((list price - actual price) / (list price)) * 100% 例如: ((25 - 10) / 25) * 100% = 60% 我知道

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

我需要计算出用户以第二种价格购买时节省的百分比

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

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

100%折扣价/全价

这个公式应该是
(原始-折扣)/original
。i、 e.(365-165)/365=0.5479

((list price - actual price) / (list price)) * 100%
例如:

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

我知道这是相当古老的,但我认为这是最好的任何把这个。我发现了一篇来自雅虎的帖子,上面有一个很好的解释:

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.

我知道这是一个非常古老的问题,但这是我如何计算两个数字之间的百分比差:

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

这是带有反转选项的函数

它将返回:

  • “change”-可用于模板中css类的字符串
  • “结果”-简单的结果
  • “格式化”-格式化结果

函数getPercentageChange($oldNumber、$newNumber、$format=true、$invert=false){ $value=$newNumber-$oldname; $change=''; $sign=''; $result=0.00; 如果($invert){ 如果($value>0){ //上升 $change='up'; $sign='+'; 如果($oldNumber>0){ $result=($newNumber/$oldname)*100; }否则{ $result=100.00; } }elseif($value<0){ //下降 $change='向下'; //$value=abs($value); $result=($oldname/$newNumber)*100; $result=abs($result); $sign='-'; }否则{ //不变 } }否则{ 如果($newNumber>$oldname){ //增加 $change='up'; 如果($oldNumber>0){ $result=($newNumber/$oldname)-1)*100; }否则{ $result=100.00; } $sign='+'; }elseif($oldNumber>$newNumber){ //减少 $change='向下'; 如果($oldNumber>0){ $result=($newNumber/$oldname)-1)*100; }否则{ $result=100.00; } $sign='-'; }否则{ //不变 } $result=abs($result); } $result\u formatted=数字\u格式($result,2); 如果($invert){ 如果($change=='up'){ $change='向下'; }elseif($change=='down'){ $change='up'; }否则{ // } 如果($sign=='+')){ $sign='-'; }elseif($sign='-'){ $sign='+'; }否则{ // } } 如果($格式){ $FORMATED='.$sign'.$result_FORMATED'.%'; }否则{ $FORMATED=$result\U FORMATED; } 返回数组('change'=>$change,'result'=>$result,'formatted'=>$formatted); }
我为我的一个应用程序做了相同的百分比计算器,如果你选择“年度计划”而不是“月度计划”,我们需要显示保存的百分比。。它可以帮助你在给定的时间内节省一定数量的钱。我已经把它用于订阅了

每月支付一年——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)
将百分比四舍五入到双精度上的扩展用法:

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
   }
}
我附上了理解相同的图像

如果总数为:200,谢谢 得到50分

然后取50/200的百分比为:


(50/200)*100=25%

从技术上讲,您可以将100相乘,然后打印%符号。“100%”的值并不总是与“100”的值相同。事实上。。。百分比的计算对我来说总是很有趣“%”表示“百分比”表示“1/100”,而“100%”表示100/100=1。因此,乘以“100%”与乘以1相同:它保持值不变。因此,0.6=0.6*100%=60%,类似地,1/2=1/2*100%=50%,等等@shrevatsar-你想让我把问题重新表述为“计算两个数字之间节省百分比的PHP公式”,然后人们可以写
$diff=($listPrice-$actualPrice)/($listPrice))*100无论哪种方式,如果不使用公式,你都无法真正编写程序,因此,这个问题与编程有关。我认为如果你明确说明它与编程有关,它会得到改进。如前所述,这纯粹是一个数学问题。我确实在math.stackexchange.com上问过,但这似乎有点不合适,因为我甚至找不到标签“公式”或“百分比”@克拉斯:这就是我们想要的。虽然这个代码片段可以解决这个问题,但它确实有助于提高你文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满你的代码,这会降低代码和解释的可读性!解释得很好,兄弟
 let savingsPercentage = self.calculatePercentage(monthly: Double( monthlyProduct.price), yearly: Double(annualProduct.price))
 self.btnPlanDiscount.setTitle("Save \(Int(savingsPercentage))%",for: .normal)
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
   }
}