我正在尝试找出价格的折扣金额。
商品原价为£50.00,现售价为£25.00,折扣为50%。
但是,当我在PHP中使用以下公式时,它并没有给出正确的折扣百分比。
$percent = $rowx->Orgprice - $rowx->SalePrice / 100;
$percent = 50 - 25 / 100 = 49.75;
$percent = 50 - 20 / 100 = 49.8;
所有以上百分比都是错误的。
使用以下公式计算折扣百分比:
折扣百分比 = (原价 - 折扣价) / 原价 * 100
将其转换为代码应该是这样的:
$percent = (($rowx->Orgprice - $rowx->SalePrice)*100) /$rowx->Orgprice ;
selling price = actual price - (actual price * (discount / 100))
举个例子,如果 (实际价格) = $15,(折扣) = 5%
selling price = 15 - (15 * (5 / 100)) = $14.25
1 - (销售价格 / 原价) * 100
,因此:$percent = 1 - ($rowx->SalePrice / $rowx->Orgprice) * 100;
$percent = 1 - (25 / 50) * 100 = 50
我希望以下代码能解决你的问题:
$percent = 100 * $rowx->SalePrice / $rowx->Orgprice;
echo $percent;
另一个选项:
原始售价 = 15, 折扣 = 5%
Original selling price *(1-discount) = sale price,
15*(1-.05)=14.25
Sale price/Original selling price -1
14.25/15-1=-.05 so 5% discount