Working out the new price from a %

时间:2016-06-07 11:28:57

标签: php

I have tried to do this myself, it seems fairly simple i must be over complicating it, i have the following code:

// get the % we make
$row = DB::getInstance()->selectValues('SELECT * FROM `profit`');  
$per = $row['profit_percentage'];
$pro = $per * 100 - ((str_replace("$", " ", trim($offer->children('campinfo', true)->amount) / 100)));

The value of $per = 60 this value $offer->children('campinfo', true)->amount can contain any value like: 0.55 or 25.34 what i'm trying to do is deduct 60 (the percentage) off the value of $offer->children('campinfo', true)->amount

I'm going round in circles

any help would be appreciated.

1 个答案:

答案 0 :(得分:2)

这归结为简单的数学:
提供60%的折扣与仅收取40%的折扣相同 所以你要做的就是计算$price * 0.4

Pro tipp:不要将您的百分比存储为{0..100},而是存储为{0..1}。这样您就可以将代码重写为(1 - $per) * $price