Page 1 of 2

Maths advice please-deducting first set of values, then more

Posted: Fri Aug 24, 2012 7:58 am
by simonmlewis
Ok, we are doing a valuation thing.

So you have a price of £100.00.
At the first stage the reduction might be 50%.

Then the second stage takes that value and deducts a further 20%.

Example: 100.00 - 50%. 50.00.
50.00 - 20% = 10.00.

I cannot seem to work out the best way to do this.
Here it is so far.

Code: Select all

$result = mysql_query ("SELECT * FROM products WHERE id = '$id'");
          while ($row = mysql_fetch_object($result))
          {
          $value = $row->rrp;
          $rrp = $row->rrp;
          } mysql_free_result($result);
          
          if ($age == "0-1") { $value = $value / 100 * 50;
          $value = $rrp - $value;}
          if ($age == "1-2") { $value = $value / 100 * 55;
          $value = $rrp - $value;}
          if ($age == "2-3") { $value = $value / 100 * 60;
          $value = $rrp - $value;}
          if ($age == "3-4") { $value = $value / 100 * 65;
          $value = $rrp - $value;}
          if ($age == "4+") { $value = $value / 100 * 70;
          $value = $rrp - $value;}
 }        
echo "Age: $value";
if ($condition == "good") 
{
  $value = $value / 100 * 20;
  echo "$value";
}
echo "Conditon: $value";
}
Any and all suggestions gratefully received.

Simon :dubious:

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 11:58 am
by social_experiment
You could swop out the if statements for a switch statement

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 12:06 pm
by simonmlewis
Ok I like that, but how would I use a Switch statement to do mathematics with percentages?

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 12:12 pm
by social_experiment
I would guess in the same way you are doing them now; the switch statement would only clean up the if statements not make any changes to the equations themselves.

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 12:16 pm
by simonmlewis
Ok. So is there no simpler way taking off a percentage? I did try $value - 50%; but it got upset!!

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 12:20 pm
by requinix
simonmlewis wrote:50.00 - 20% = 10.00.
Uh, what?

Code: Select all

100.0 * (1 - 0.5) = 50.0
50.0 * (1 - 0.2) = 40.0
Note the "1 minus" part - you're missing that, and without it you're actually doing a 50% (okay it's the same amount, coincidence) and 80% reduction. Incidentally, that's what you 50-20% example is doing so now I'm not even sure which one you're trying to do.

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 12:28 pm
by simonmlewis
Sorry I don't understand that (1 - 0.5)?
If I wanted to reduce a value by 50%, would I do this?
$value = $value x (1 - 0.5); ??

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 2:42 pm
by requinix
simonmlewis wrote:Sorry I don't understand that (1 - 0.5)?
If I wanted to reduce a value by 50%, would I do this?
$value = $value x (1 - 0.5); ??
Yes (but 50% isn't a good example).

If you wanted to reduce the price by 20% then you need to subtract 20%.

Code: Select all

$value - ($value * 0.20)
Or written a different way with the $value factored out,

Code: Select all

$value * (1 - 0.20)
If you just multiply by 20% then you are "keeping" that 20%.

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 2:48 pm
by simonmlewis
$value = 100.00;
$value - ($value * 0.50);
echo "$value";

That would produce 50.00 would it?

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 4:10 pm
by requinix
No, because you don't assign the result of the subtraction back into $value.

Code: Select all

$value = 100.00;
$value = $value - ($value * 0.50);
echo $value;
would give the result you want.

Re: Maths advice please-deducting first set of values, then

Posted: Fri Aug 24, 2012 5:57 pm
by social_experiment
simonmlewis wrote:Then the second stage takes that value and deducts a further 20%.
the snippet below does what you have in mind; hth

Code: Select all

<?php
$innitial = 70;	
$percentageAry = array(50, 20, 10);
//
echo $innitial . '<br />';
foreach ($percentageAry as $percentage) {
	$new = ($innitial / 100) * $percentage;
	$result = $innitial - $new;
	echo $percentage . '% of ' . $innitial . ' is ' . $new ;
	$innitial = $new;
	echo '<br />';
	}
/* Results
------------
70
50% of 70 is 35
20% of 35 is 7
10% of 7 is 0.7
------------
65
50% of 65 is 32.5
20% of 32.5 is 6.5
10% of 6.5 is 0.65
*/
?>

Re: Maths advice please-deducting first set of values, then

Posted: Tue Aug 28, 2012 3:59 am
by simonmlewis
Perfect. How is the last "longer" comment going to be better, as it looks like a lot of effort for the same basic result?

... respectfully!

Re: Maths advice please-deducting first set of values, then

Posted: Tue Aug 28, 2012 4:05 am
by social_experiment
simonmlewis wrote:How is the last "longer" comment going to be better, as it looks like a lot of effort for the same basic result?
could you explain which comment you are refering to here?

Re: Maths advice please-deducting first set of values, then

Posted: Tue Aug 28, 2012 4:08 am
by simonmlewis
Yours.
As I said, the last one, which was the one before mine.

Re: Maths advice please-deducting first set of values, then

Posted: Tue Aug 28, 2012 4:17 am
by social_experiment
i see, if you have a different approach that's much quicker then go with it, the snippet i wrote is how i'd approach the issue :)