Display page views count in K and M format in php -
i created page_views_count script in php display page views count in k format.
the script bellow working doesn't give expected result.
this script :
<?php $views=1060; if($views > 1000) {$views_count=$views *1/1000; echo "$views_count k views";} else{echo $views;} this script shows views count way :
999 = 999 page views 1050= 1.05 k page views 1060 =1.06 k page views 2300= 2.3 k page views i want show results in following format :
999 = 999 page views 1050= 1 k page views 1060 =1.1 k page views 2300= 2.3 k page views 2354= 2.4 k page views does know how solve this? on issue appriciated.
kind regards!
starkeen.
php round() function , php_round_half_up constant solved problem.
<?php $views=1060; if($views > 1000) {$views_count=$views *1/1000; $views_k=round($views_count,php_round_half_up); echo "$views_k k views";} else{echo $views;} ?> output :
999 = 999 page views 1060= 1 k page views 1160 =1.2 k page views 2300= 2.3 k page views 2354= 2.4 k page views hope helps in future.
Comments
Post a Comment