【发布时间】:2012-01-22 08:45:28
【问题描述】:
我有以下用于计算百分比减少或增加的 PHP:
function CalculatePercentageIncrease( $nLastMonthPeriod, $nCurrentPeriod ) {
if ( !is_numeric( $nLastMonthPeriod ) || !is_numeric( $nCurrentPeriod ) )
return 0;
if ( $nLastMonthPeriod == 0 )
return 0;
$nLastMonthPeriod = intval( $nLastMonthPeriod );
$nCurrentPeriod = intval( $nCurrentPeriod );
$nDifference = ( ( ( $nCurrentPeriod - $nLastMonthPeriod ) / $nLastMonthPeriod ) * 100 );
return round( $nDifference );
}
我想知道的问题是如果$nLastMonthPeriod 是 0 而$nCurrentPeriod 是 10 那么它应该返回 100 而不是 0?
【问题讨论】:
标签: php math divide-by-zero