Jump to content
MakeWebGames

PHP: Percentage Mistakes and the fix..


LordDan

Recommended Posts

Okay, I'm posting this here because it seems to be a common mistake. It seems the majority of Percentage functions I have ever seen in PHP are actually wrong because of a simple mistake... I've also seen this common mistake in Open Source projects dotted around SourceForge..

For my examples, I am going to get 12% of 144.

This function below is common, but it's also incorrect..

function getPercentage( $perc, $val )
{
  return round( ( $perc*100 ) / $val );
}

 

This is incorrect because it returns 8 (Or 9 if you use ceil instead of number_format)... 12% of 144 is not 8, it's 17 (17.28 to be precise). So what went wrong? Simply put, the * and / are the wrong way round..

 

function getPercentage( $perc, $val )
{
  return round( ( $perc/100 ) * $val );
}

 

This will return the 17 we're looking for..

Math:

12% is 12/100

So: (12/100) x 144 = (0.12) x 144 = 17.28

Why wasn't this realized during testing?

Simple, most poeple test an easy percentage by getting a percentage out of 100 (10% of 100) because the answer is obvious and in this case, both functions actually return the correct value of 10%. It's only when you want a percentage of something other than 100 you'll start to get incorrect results. But, because it was correct the first time, you assume it's correct and use it none the wiser.

 

Anyway, just posting this because it is a common mistake. Check your percentage functions for 12 of 144, if you get the correct value, well done for paying attention in school, if not, you can now correct it. I'm interested to see your results :P

(PS: I'm no math genius, so if I'm wrong, show me the math :thumbup: )

------------------

Test Case for those who care..

echo getPercentage( 12, 144 ).'
';
echo getPercentage( 200, 1500 ).'
';
echo getPercentage( 268, 1678 ).'
';
echo getPercentage( 6789, 1000 ).'
';

// 12% of 144 = 17
// 200% of 1500 = 3000
// 268% of 1678 = 4497
// 6789% of 1000 = 67890
Link to comment
Share on other sites

ah cool like my function which gathers the average(mean).

<?php
function avgval($avg_vals) {
if ( is_array($avg_vals) && count($avg_vals) > 1 ) {
       $return_vals = ( array_sum($avg_vals) / count($avg_vals) );
} elseif ( is_array($avg_vals) && count($avg_vals) == 1 ) {
       $return_vals = current($avg_vals);
} else {
       $return_vals = FALSE;
}
  return $return_vals;
}
  echo avgval(array(6,11,7)); // outputs 8
  echo avgval(array(6)); // outputs 6
?>

Working with string:

<?php
function avgvals($avg_vals,$avg_delimiter=',') {
if ( (is_string($avg_vals) && strlen($avg_vals) > 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
   $average_vals = explode($avg_delimiter, $avg_vals);
       $return_vals = ( array_sum($average_vals) / count($average_vals) );
} elseif ( (is_string($avg_vals) && strlen($avg_vals) <= 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
       $return_vals = $avg_vals;
} else {
       $return_vals = FALSE;
}
  return $return_vals;
}
  echo avgvals('6,11,7'); // outputs 8
  echo avgvals('6-11-7', '-'); // outputs 8
  echo avgvals('6'); // outputs 6
?>

Source of script: (php.net)

Link to comment
Share on other sites

ah cool like my function which gathers the average(mean).
<?php
function avgval($avg_vals) {
if ( is_array($avg_vals) && count($avg_vals) > 1 ) {
       $return_vals = ( array_sum($avg_vals) / count($avg_vals) );
} elseif ( is_array($avg_vals) && count($avg_vals) == 1 ) {
       $return_vals = current($avg_vals);
} else {
       $return_vals = FALSE;
}
  return $return_vals;
}
  echo avgval(array(6,11,7)); // outputs 8
  echo avgval(array(6)); // outputs 6
?>

Working with string:

<?php
function avgvals($avg_vals,$avg_delimiter=',') {
if ( (is_string($avg_vals) && strlen($avg_vals) > 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
   $average_vals = explode($avg_delimiter, $avg_vals);
       $return_vals = ( array_sum($average_vals) / count($average_vals) );
} elseif ( (is_string($avg_vals) && strlen($avg_vals) <= 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
       $return_vals = $avg_vals;
} else {
       $return_vals = FALSE;
}
  return $return_vals;
}
  echo avgvals('6,11,7'); // outputs 8
  echo avgvals('6-11-7', '-'); // outputs 8
  echo avgvals('6'); // outputs 6
?>

Source of script: (php.net)

Thats a pretty nice function. Basic math yet i would of never thought of it lol.

Link to comment
Share on other sites

ah cool like my function which gathers the average(mean).
<?php
function avgval($avg_vals) {
if ( is_array($avg_vals) && count($avg_vals) > 1 ) {
       $return_vals = ( array_sum($avg_vals) / count($avg_vals) );
} elseif ( is_array($avg_vals) && count($avg_vals) == 1 ) {
       $return_vals = current($avg_vals);
} else {
       $return_vals = FALSE;
}
  return $return_vals;
}
  echo avgval(array(6,11,7)); // outputs 8
  echo avgval(array(6)); // outputs 6
?>

Working with string:

<?php
function avgvals($avg_vals,$avg_delimiter=',') {
if ( (is_string($avg_vals) && strlen($avg_vals) > 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
   $average_vals = explode($avg_delimiter, $avg_vals);
       $return_vals = ( array_sum($average_vals) / count($average_vals) );
} elseif ( (is_string($avg_vals) && strlen($avg_vals) <= 2) && (is_string($avg_delimiter) && !empty($avg_delimiter)) ) {
       $return_vals = $avg_vals;
} else {
       $return_vals = FALSE;
}
  return $return_vals;
}
  echo avgvals('6,11,7'); // outputs 8
  echo avgvals('6-11-7', '-'); // outputs 8
  echo avgvals('6'); // outputs 6
?>

Source of script: (php.net)

Got any functions that will give the median and mode?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...