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

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!

Get our RSS Feed!

Sponsors

Related Posts

One Reply to "Calculating value from percent"

  1. Esse script é simples e direto.

    Muito Bom!

    tags
    calculo de porcentagem com php exemplo script

Leave a Reply


* = required fields

(will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


  

CommentLuv Enabled