我试图找出价格的折扣金额。
项目成本WAS£50.00售价£25.00折扣=%50
但是,当在PHP中使用以下公式时,它不能为我提供正确的折扣百分比。
$percent = $rowx->Orgprice - $rowx->SalePrice / 100;
$percent = 50 - 25 / 100 = 49.75;
$percent = 50 - 20 / 100 = 49.8;
Run Code Online (Sandbox Code Playgroud)
以上所有百分比都是错误的。
Reu*_*_v1 11
使用以下公式计算折扣率:
折扣%=(原价-销售价)/原价* 100
将其转换为代码,应该类似于:
$percent = (($rowx->Orgprice - $rowx->SalePrice)*100) /$rowx->Orgprice ;
Run Code Online (Sandbox Code Playgroud)
小智 5
正确的公式是1 - (sale price / original) * 100,所以:
$percent = 1 - ($rowx->SalePrice / $rowx->Orgprice) * 100;
$percent = 1 - (25 / 50) * 100 = 50
Run Code Online (Sandbox Code Playgroud)
小智 5
selling price = actual price - (actual price * (discount / 100))
Run Code Online (Sandbox Code Playgroud)
例如,如果(实际价格)= 15 美元,(折扣)= 5%
selling price = 15 - (15 * (5 / 100)) = $14.25
Run Code Online (Sandbox Code Playgroud)