Inside-Out - Web Design & Development 4 Beginners

Here is an incredibly short, sweet and simple php function that I use all of the time.  This function provides you with a rounded percentage, base between two numbers:

<?php
function Get_Percent($number,$total)
{
$percentage = round($number * 100 / $total).”%”;
echo $percentage;
}

// Usage
echo “The percentage of people who came to my party was “.Get_Percent(19,23);
?>

For example, if I invited 23 friends to a party and only 19 turned up, to get the percentage I would use Get_Percent(19,23). This would parse as ‘83%’ – phew, I still have the majority in the real friends stake!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • BlinkList
  • De.lirio.us
  • Furl
  • Reddit
  • StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Spam protection by WP Captcha-Free