[PHP] Simple percentage calculator
Posted: Tue Jul 27, 2010 10:00 pm
So I've written a math helper to help me out with some of the calculations needed for the PBBG I'm making, but this function is pretty helpful for anyone as it can help you to calculate percentage of HP, MP or what have you. (I'm not sure if you've already been over this in the tutorials, as I haven't gone through them all, but it could be useful nonetheless)
Here's the code: (file called math_helper.php)
Usage example:
Anyway, hope someone finds it useful!
Here's the code: (file called math_helper.php)
Code: Select all
if ( ! function_exists('percentage'))
{
function percentage($min, $max)
{
$count1 = $min / $max;
$count2 = $count1 * 100;
$count = number_format($count2, 0);
return $count;
}
}Usage example:
Code: Select all
include 'math_helper.php'
$maxhp = '15';
$current_hp = '10';
echo '<div style="width: 110px;background-color:red;">';
echo '<div style="width:'.percentage($current_hp, $maxhp).'%; background-color: green">';
echo $current_hp.'/'.$maxhp.'</div></div>';