本文介绍了PHP中的百分比计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图计算一个百分比以下(顺便说一句,我需要四舍五入,但是它一直显示100.如何使用以下示例在php中计算百分比:
I am trying to caluclate a percentage below (which by the way I need to be rounded up, but it keeps displaying 100. How can I calculate percentage in php using below's example:
$totalmarks += (int)$questionData['questionmarks'];
$studentmarks += (int)$questionData['studentmark'];
$percentage = $studentmarks / $studentmarks * 100;
echo $percentage;
推荐答案
您的公式有误.您正在单独分隔studentmarks
.它必须除以totalmarks
.目前它就像说$percentage = 1/1 * 100
;始终为100.
Your formula is wrong. You are dividing studentmarks
by itself. It has to be divided by totalmarks
. Currently its like saying $percentage = 1/1 * 100
; which will always result in 100.
$percentage = ($studentmarks / $totalmarks) * 100;
$percentage = round($percentage,2);
这篇关于PHP中的百分比计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!