我无法从 PHP 的 round() 中获得我需要的结果


I can't get the result I need from PHP's round()

我无法让round()函数正常工作。这是我目前正在做的事情:

if ($con2 == "sf") {
    $formula = $num * 0.00694444;
    $formula = round($formula);
    $result = $num . $si . "<br>RESULT: " . $formula . $sf;
};

使用此代码,当$num为 144 时,结果为 0.99999936。但是,我需要将其四舍五入为 1。为什么会发生这种情况,我怎样才能让round()按照我期望的方式行事,而不是这种违反直觉的方式?

尝试:

$formula = round($formula, 0);

好的 - 所以你能试试这个脚本,看看会发生什么,当我输入 144 时,它正在与你正在寻找的行为一起工作:

<?
if($_POST){
    $num=$_POST["number"];
    $formula = $num * 0.00694444;
    $formula = round($formula);
    echo $formula;
} else {
?>
<form action="test.php" method="post">
<input type="text" name="number" />
<input type="submit" />
</form>
<?
}
?>

指定所需的小数位数:

$formula = $num * 0.00694444;
$formula = round($formula,0);
echo $formula;