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.
Add Feedback (view all)
Leave feedback
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.
- Podcasts in next iTunes version?
- iTunes playlist ideas: baseball theme
- Zeep Mobile enters the SMS fray
Popular Pages
- Fast rounded corners in Photoshop (5984 recent visits)
- PHP – passing variables across pages (2186 recent visits)
- JavaScript set selected on load (1824 recent visits)
- Removing all child nodes from an element (1311 recent visits)
- iPod songs out of order? (1074 recent visits)
- Firefox 3 smart address bar: wildcard search (988 recent visits)
- Britney - Everytime piano tab (922 recent visits)
- MySQL LEFT JOIN syntax (768 recent visits)
- Breathe Me - Sia (690 recent visits)
- Tumblr: how blogging should be (569 recent visits)
Similar Entries
- PHP: Skipping index page call in URL (34 recent visits)
- PHP project: convert times to numbers (80 recent visits)
- PHP – passing variables across pages (2186 recent visits)
- Install Apache, PHP, MySQL on Windows (25 recent visits)
- Code mnemonics: PHP implode/explode (76 recent visits)
- Swap banner image with CSS and PHP (173 recent visits)
Stats
367 unique visits since August 2008
Recent Referrers (click)
- php divide percent
- calculate percentage savings
- how do i determine percentage of savings
- calculation of percentage to money
- calculate percentage
- math show percent savings
- php calculate percentage
- savings percentages
- savings percentage formula
- percent savings formula
- percentage savings
- calculating percentage savings
- calculate percent savings
- php percentage calculation
- calculate a savings percentage
- how to figure out percentage of savings
- calculate percentage rounded
- php calculate percentage
- how we calculate our percentage
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.