php中的百分比计算

时间:2013-03-08 06:34:18

标签: php math

我正在尝试计算一个下面的百分比(顺便说一下,我需要四舍五入,但它会一直显示100.如何使用下面的示例计算php中的百分比:

    $totalmarks += (int)$questionData['questionmarks'];
    $studentmarks += (int)$questionData['studentmark'];
    $percentage = $studentmarks / $studentmarks * 100;

echo $percentage;

4 个答案:

答案 0 :(得分:6)

你的公式错了。您要将studentmarks分开。它必须除以totalmarks。目前它就像在说$percentage = 1/1 * 100;这将永远导致100。

$percentage = ($studentmarks / $totalmarks) * 100;
$percentage = round($percentage,2);

答案 1 :(得分:1)

最简单的方式(恕我直言)总是划分总标记 100 然后乘以 你想要(学生)的百分比

$percentage = ($totalmarks / 100) * $studentmarks

答案 2 :(得分:1)

我希望此代码可以帮助您:)

$totalmarks += (int)$questionData['questionmarks'];
$studentmarks += (int)$questionData['studentmark'];
$percentage = ($studentmarks /   $totalmarks) * 100;
echo $percentage;

答案 3 :(得分:0)

要进行整理,您需要使用ceil()

echo ceil(($questionData['studentmark'] / $questionData['questionmarks']) *100);
相关问题