Calculating value from percent
Posted on August 31, 2008, Filled under PHP,
Bookmark it
This short tutorial is aimed to help you how to calculate the percent value from a total. Let’s suppose you have a e-commerce site and you offer discounts to the customers. You need to show them how much is the discount value worthing.
Here’s a sample from an order total:
Notebook’s price: $1,300
Our special discount: 30% (-$390)
Final price: $910
Our special discount: 30% (-$390)
Final price: $910
Here’s the script which calculates the special discount value and the final price:
<?php $percent = '30'; // without % $total = '1300'; // initial value /* Calculate $percent% from $total */ $discount_value = ($total / 100) * $percent; $final_price = $total - $discount_value; // Format numbers with number_format() $total = number_format($total); $discount_value = number_format($discount_value); $final_price = number_format($final_price); echo "Notebook's price: $".$total."<br />Our special discount: <strong>". $percent."%</strong> (-$".$discount_value.")<br />Final price: $".$final_price; ?>
Any comments and suggestions regarding this script are welcomed.
Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!
- August 31, 2008
- article by Gabriel C.
- 4 comments
Related Posts
Calculate percent from valueat August 31, 2008 with 2 comments
How to Create a PHP Word Popularity Scriptat November 7, 2008 with 6 comments
PHP: Calculate the Size, Number of Files & Folders of a Directoryat September 24, 2008 with 3 comments

4 Replies to "Calculating value from percent"
September 18, 2008 at 11:11 AM
Esse script é simples e direto.
Muito Bom!
tags
calculo de porcentagem com php exemplo script
May 8, 2010 at 4:19 AM
Muy bueno
July 5, 2010 at 2:33 PM
Awesome script. Quick question though;
What if the total includes change? Like I have a total that is $45.33 or something? Right now, the percentage only returns a dollar total with no change. Suggestion?
July 16, 2010 at 9:51 AM
It’s a simple solution, but with multiple product I think this script require some modification great.