在 PHP 中 while 循环之后舍入一个值


Round a Value after While Loop in PHP

嘿,使用以下代码,我正在尝试将变量四舍五入$CelNum,由于我是PHP新手,我不确定如何正确执行此操作,如果您可以帮助我,我将不胜感激。

<head>
    <title>Temp Conversion</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?
$FarNum = 0;
$CelNum = 0;
while ($FarNum <= 100)
{
$CelNum = ($FarNum - 32)*5/9;
echo "<p>$FarNum = $CelNum</p>";
$FarNum++;
}

我假设您正在尝试打印 1F - 100F 的摄氏度值。您必须在循环中round CelNum的值才能正常工作:

while ($FarNum <= 100)
{
    $CelNum = round( ($FarNum - 32)*5/9 );
    echo "<p>$FarNum = $CelNum</p>";
    $FarNum++;
}

如果你想更准确,你可以把它四舍五入到小数位:

$CelNum = ($FarNum - 32)*5/9;
$CelNum = round($CelNum, 2); // eg: -17.78

现场观看!

要使 CelNum 四舍五入,请使用 round 函数:

$CelNum = round(($FarNum - 32) * 5 / 9);

为了找到这个功能,我只是去了php网站并搜索了"round"。然后我找到了函数本身。

哦,顺便说一下,不要使用<?.更喜欢<?php:

<?php