如何將浮動百分比數值轉換爲PHP中的整數百分比數值?如何將浮點百分比值轉換爲整數百分比?
例如:3.8461538461538%= 4%
更新: 這裏是我的代碼,我編寫得到的百分比值,但我得到的十進制值,但我需要它沒有十進制值。
$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo $percent ;
echo "%";
如何將浮動百分比數值轉換爲PHP中的整數百分比數值?如何將浮點百分比值轉換爲整數百分比?
例如:3.8461538461538%= 4%
更新: 這裏是我的代碼,我編寫得到的百分比值,但我得到的十進制值,但我需要它沒有十進制值。
$total = array_sum($total_count);
$percent = ($occurs/$total)*100;
echo $percent ;
echo "%";
<?php number_format(3.8461538461538); ?>
可以在S指定第二個參數是小數點的數量(默認值爲0)。
@Jennifer number_format( )用於轉換數字格式。具體來說,創建以逗號分隔的貨幣值。因此,它會在很多方面比圓形()更重。 我不明白爲什麼這是'正確'的答案,而不是round()。 :-) – Niranjan
使用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文檔](http://php.net/manual/en/function.round.php) – Niranjan
你試過了什麼? (什麼是*類型的值?字符串?一個數字?) – 2012-05-03 19:36:36