舍入 PHP 页面中的所有浮点值


Round all Float Value in a PHP Page

有一种方法可以将 PHP 脚本输出中的所有浮点数舍入?

例如:

$var_float1 = 585.584826684587;
$var_float2 = 675.457567845657;
$var_float3 = 123.9058021466589;
echo $var_float1;
echo $var_float2;
echo $var_float3;

我希望所有浮点输出都四舍五入,每次都不使用 Round() 函数,就像页面的设置

您可以使用自定义函数:

function round8($input){
    $output = round($input, 8);
    return $output; 
}
$better = round8(585.8958448484);
echo $better;

你能不使用 round() 吗?

$x = round(3.141592, 3);
echo $x; // outputs 3.142

http://www.php.net/manual/en/function.round.php

更新

如果你想为更多的浮点数这样做。

$arr = [3.1415, 346.78454, 346.255];
foreach($arr as $f) 
{
    echo round($f, 3);
}

不,浮点数没有关于它将如何表示的概念,它只是一个数字(不过,尝试减少代码的荣誉!你必须告诉它它将如何表示。

如果你想显示号码,那么你将不得不,在某个时候打电话给round()number_format。是通过自定义函数还是循环(如其他回答者所提到的)取决于您。