Mathematical equation - help pls!

Hi guys,

I’ve been trying to modify a script I am using so that instead of just displaying prices, it will also display the discount as a percentage.

Example:
X = $300 - Y = $200 So the equation (((X - Y) / X) * 100) = Z should return 33.33 as the value of Z, which works perfectly if carried out manually.

However I tried putting this into php and all it keeps returning is “0” even though the other variables meant it should have returned values of 15 and above…?

This is the code I used, but I can’t spot the error…

[php]$discount = ((($listprice - $price) / $listprice) * 100);
$content = str_replace("{discount}", $discount, $content);[/php]

If anybody can offer a little advice I’d be very grateful.
Cheers Taffy

hello Taffy1957,

use below code here i have created a function for calculate percentage.

[php]
//Put this function in your php file and call it when you need.
function saving($listprice,$price)
{
return $discount = round(($listprice - $price) / $listprice * 100, 2);
}

//use below code
$listprice = 300; $price=200;
$discount = saving($listprice ,$price);

//Instead of below code
$discount = ((($listprice - $price) / $listprice) * 100);
[/php]

i hope this will helpful for you…
Reply your feedback
SR

Hello SP,
thanks for the assistance :wink:
Not quite sure how to apply the code you listed, however let me just say that in the template I am using, various variables are called by enclosing the name of the variable in curly brackets like so {discount}
Now since posting earlier, I have been racking my brain trying to find the answer myself (best way to learn eh), anyway by using the following code in a test.php & opening in my browser, it does return the correct value on the screen;
[php]<?php
$listprice = 495;
$saving = 175;
//Above values are dynamically assigned for each new post / product
$discount = round (($saving / $listprice) * 100);
$content = str_replace("{discount}", $discount, $content);
echo $discount; ?>[/php]

The problem I now have is that every time I add this code to the script, it returns a zero as the discount value irrespective of what the other values are.
I know I’m close to solving this, but this last problem is doing my head in!

Hello Taffy1957, if there is any common function file than put the function in that file and include this file in your code. than call that function. if you do not have any common function file than create a file having name common_function.php save below code as common_function.php [php] // Contain common functions function saving($listprice,$price) { return $discount = round(($listprice - $price) / $listprice * 100, 2); } [/php]

Now you have to simply include this file in you code at top
[php]

<? include('common_function.php'); //include('put file path here where the common_function.php'); $discount = saving($listprice ,$price); $content = str_replace("{discount}", $discount, $content); echo $discount ?>

[/php]
Reply your feedback
SR

Sponsor our Newsletter | Privacy Policy | Terms of Service