百分比计算在php


Percentage calculation in php

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

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

你的公式错了。你把studentmarks除以它本身。它必须除以totalmarks。现在它就像说$percentage = 1/1 * 100;结果总是100。

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

希望这段代码能帮到你:)

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

最简单的方法(IMHO)是总是总分除以100然后值相乘 您想要的百分比学生

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

四舍五入需要使用ceil()

echo ceil(($questionData['studentmark'] / $questionData['questionmarks']) *100);