Issue
I created a page_views_count script in php to display the page views count in K format.
The script bellow is working but it doesn’t give expected result.
This is the 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 this 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 it to show the 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 someone know how to solve this? Any help on this issue would be much appriciated.
Kind Regards!
Starkeen.
Solution
PHP round() function and PHP_ROUND_HALF_UP constant solved my 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 this helps someone in future.
Answered By – Amit Verma
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0