Calculate savings percentage with PHP May23 '05

On many e–commerce sites, often times you will be presented with how much money you could be saving (and the percentage), if you purchase the particular product:

This is a great approach to presenting the customer with important "buying information," and can often help them make that final decision of "purchase, or forget."

For Web Developers to display this information, a small mathematical formula is needed.

And, with the help of PHP, you never have to worry if your numbers are accurate. PHP takes care of it all.

Real–world example

Let’s say you have a product that you want to sell for $49.00. That price is a discount from $65.00.

Calculating the amount of savings is easy (subtract the original price from the discount price), but how do we obtain the percentage?

Anyone with a small knowledge of math would tell you to divide the difference by the original.

This is indeed accurate, and then there’s one more step: multiply that result by 100.

So, for our example, we’d subtract 49 from 65, and we end up with 16. Then, we take 16, and divide it by 65 (the original price), and we get an extremely long decimal number. It is something like 0.2461538.... If we then multiply that number by 100, we end up with a more understandable digit: 24.61538...

So, as you can see, the savings percentage for our example numbers is somewhere around 24.6%.

Unfortunately, unless we intentionally round this number, a script result (PHP, for example) will still output the incredibly long decimal.

PHP example

For our PHP example, we work with a few simple variables:

$original_price
Contains the original product price.
$discount_price
Contains the discounted price.
$savings
Contains the amount of money saved.
$savings_percentage
Contains the actual percentage of money saved.

We will assume that the $original_price and $discount_price variables have already been set.

Set both the $savings and $savings_percentage variables like this:

$savings = $original_price - $discount_price;

$savings_percentage = round( ($savings/$original_price)*100, 0 );

Most of it is straight–forward math. The PHP round function is what helps remove excess numbers after the decimal point.

For our real–world example, the result would output 24%, which is nice and round.

Categories: PHP , Tutorials

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

Thanks... My calculations to add UK VAT @ 17.5% to a delivery charge were wrong, but your blog post has provided some answers. Basi ... Read more.

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Popular Pages

  1. Fast rounded corners in Photoshop (5984 recent visits)
  2. PHP – passing variables across pages (2186 recent visits)
  3. JavaScript set selected on load (1824 recent visits)
  4. Removing all child nodes from an element (1311 recent visits)
  5. iPod songs out of order? (1074 recent visits)
  6. Firefox 3 smart address bar: wildcard search (988 recent visits)
  7. Britney - Everytime piano tab (922 recent visits)
  8. MySQL LEFT JOIN syntax (768 recent visits)
  9. Breathe Me - Sia (690 recent visits)
  10. Tumblr: how blogging should be (569 recent visits)

Similar Entries

Stats

367 unique visits since August 2008

Syndicate

Advertisements