-2

如何在 PHP 中将浮点百分比数值转换为整数百分比数值?

示例:3.8461538461538 % = 4 %

更新:这是我的代码,我编码以获得百分比值,但我得到十进制值,但我需要它没有十进制值。

$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo $percent ; 
echo "%";
4

3 回答 3

3

这是通过round()完成的- 舍入到最接近的 int

<?php 
echo round('3.8461538461538'); //4
?>

还有:

floor() -- 向下舍入分数。

ceil() -- 向上取整分数。

于 2012-05-03T19:43:32.140 回答
2

使用round(). 这是一个PHP函数。

编辑: 给定代码:

$total = array_sum($total_count);
 $percent = ($occurs/$total)*100;
 echo round($percent). "%";

例如:

$number="3.8461538461538%";   //if it's a string with % in the end.
$number=substr($number, 0,-1);  //It type converts automatically here. Lazy code :)
$number_rounded=round($number);

或者

$number=3.8461538461538;  //If it's just a float.
$number_rounded=round($number);
于 2012-05-03T19:38:17.303 回答
0
<?php number_format(3.8461538461538); ?>

您可以在第二个参数中指定小数点的数量(默认为 0)。

于 2012-05-03T19:41:19.887 回答