我有以下用于计算百分比减少或增加的 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?
最佳答案
从 0 到 10 的增加不能被描述为百分比增加。您需要单独处理这种情况。
答案不是 0% 或 100%。
任何一个
关于php - 在不除以零的情况下计算百分比变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959985/