Skip to content

Instantly share code, notes, and snippets.

@softon
Created January 11, 2016 04:55
Show Gist options
  • Save softon/3dfe45157933ce2c6726 to your computer and use it in GitHub Desktop.
Save softon/3dfe45157933ce2c6726 to your computer and use it in GitHub Desktop.
A simple function to calculate percentage without checking the divisor for zero. Also includes precision point.
<?php
/**
* A simple function to calculate percentage without checking the divisor for zero.
* Also includes precision point
*
* @param $got
* @param $out
* @param int $precision
* @return float|int
*/
function calcPercentage($dividend, $divisor, $precision=0){
if($divisor<=0){
return 0;
}
return round($dividend/$divisor*100,$precision);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment