如何将浮点百分比值转换为整数百分比


How to convert float percent value to integer percent?

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

示例:3.8461538461538 % = 4 %

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

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

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

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

还有:

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

ceil() -- 向上舍入分数。

使用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);
<?php number_format(3.8461538461538); ?>

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