php - Calculating percentage changes without dividing by zero -
php - Calculating percentage changes without dividing by zero -
i have next php using calculate percentage decreases or increases:
function calculatepercentageincrease( $nlastmonthperiod, $ncurrentperiod ) { if ( !is_numeric( $nlastmonthperiod ) || !is_numeric( $ncurrentperiod ) ) homecoming 0; if ( $nlastmonthperiod == 0 ) homecoming 0; $nlastmonthperiod = intval( $nlastmonthperiod ); $ncurrentperiod = intval( $ncurrentperiod ); $ndifference = ( ( ( $ncurrentperiod - $nlastmonthperiod ) / $nlastmonthperiod ) * 100 ); homecoming round( $ndifference ); }
the problem wondering if $nlastmonthperiod
0 , $ncurrentperiod
10 should returning 100 , not 0?
if $nlastmonthperiod 0 , $ncurrentperiod 10 should returning 100 , not 0?
thats coded ...
if ( $nlastmonthperiod == 0 ) homecoming 0;
did mean?
if ( $nlastmonthperiod == 0 ) if ($ncurrentperiod>0) homecoming 100; //whatever want else homecoming 0;
php math divide-by-zero
Comments
Post a Comment