我有一个html表,它从数据库表中回显PHP值。在此特定区域中,回显的值是数字,但小数值不受控制。我知道如何将数字舍入为一个PHP变量但是如何在回显像这样的行时将这些数字舍入为3位小数?
<td><?php echo $row['test1CorrectAcc']; ?>%</td>
<td><?php echo $row['test2CorrectAcc']; ?>%</td>
<td><?php echo $row['test3CorrectAcc']; ?>%</td>
<td><?php echo $row['test4CorrectAcc']; ?>%</td>
<td><?php echo $row['test5CorrectAcc']; ?>%</td>
<td><?php echo $row['test6CorrectAcc']; ?>%</td>
<td><?php echo $row['test7CorrectAcc']; ?>%</td>
<td><?php echo $row['test8CorrectAcc']; ?>%</td>
更新
获取值的平均值的SQL语句:
UPDATE `staging`
SET `testOverallAverage` = (`test1CorrectAcc` + `test2CorrectAcc` + `test3CorrectAcc` + `test4CorrectAcc` + `test5CorrectAcc` + `test6CorrectAcc` + `test7CorrectAcc` + `test8CorrectAcc`) /
((CASE WHEN `test1CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test2CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test3CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test4CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test5CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test6CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test7CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test8CorrectAcc`<> 0 THEN 1 ELSE 0 END )
);
答案 0 :(得分:2)
您可以使用number_format
功能:
<td><?php echo number_format($row['test1CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test2CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test3CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test4CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test5CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test6CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test7CorrectAcc'], 3); ?>%</td>
<td><?php echo number_format($row['test8CorrectAcc'], 3); ?>%</td>
此外,如果您可以通过半向上舍入等方式对其进行舍入,则可以将round
函数与number_format
一起使用:
<td><?php echo number_format(round($row['test1CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test2CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test3CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test4CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test5CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test6CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test7CorrectAcc'], 3), 3); ?>%</td>
<td><?php echo number_format(round($row['test8CorrectAcc'], 3), 3); ?>%</td>
你可以使用loop for echo:
<?php
for($i = 1; $i <= 8; $i++) {
?>
<td><?php echo number_format($row['test'.$i.'CorrectAcc'], 3); ?>%</td>
<?
}
?>
如果我理解,当所有值都为零时,您会出现sql错误,因此您可以使用以下查询来编写您的查询格式。首先检查所有值是否为零,然后testOverallAverage
为零,否则与计算相同。
UPDATE `staging`
SET `testOverallAverage` = IFNULL(
(`test1CorrectAcc` + `test2CorrectAcc` + `test3CorrectAcc` + `test4CorrectAcc` + `test5CorrectAcc` + `test6CorrectAcc` + `test7CorrectAcc` + `test8CorrectAcc`)
/
NULLIF(
(CASE WHEN `test1CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test2CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test3CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test4CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test5CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test6CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test7CorrectAcc`<> 0 THEN 1 ELSE 0 END ) +
(CASE WHEN `test8CorrectAcc`<> 0 THEN 1 ELSE 0 END )
, 0)
, 0);