I need to show a page views value in the format of 1K of equal to one thousand, or 1.1K, 1.2K, 1.9K etc, if its not an even thousands, otherwise if under a thousand, display normal 500, 100, 250 etc, using PHP to format the number?
I'm using:--
function count_number($n) {
// first strip any formatting;
$n = (0+str_replace(",","",$n));
// is this a number?
if(!is_numeric($n)) return false;
// now filter it;
if($n>1000000000000) return round(($n/1000000000000),1).'T';
else if($n>1000000000) return round(($n/1000000000),1).'G';
else if($n>1000000) return round(($n/1000000),1).'M';
else if($n>1000) return round(($n/1000),1).'K';
return number_format($n);
}
BUT it does not work correctly...
If my page visted 2454 times, it shows 2.5k and if 2990, it shows 3k...
How o fix that problem??
I want to SHOW Like --> if page visited 2454 -> how to display 2.4k and if 2990 -> 2.9k, if 3000 -> 3k etc
Plz help me...
Thanks @ MonkeyZeus
Now itz DONE...
function kilo_mega_giga($n) {
if($n >= 1000 && $n < 1000000)
{
if($n%1000 === 0)
{
$formatted = ($n/1000);
}
else
{
$formatted = substr($n, 0, -3).'.'.substr($n, -3, 1);
if(substr($formatted, -1, 1) === '0')
{
$formatted = substr($formatted, 0, -2);
}
}
$formatted.= 'k';
} else
if($n >= 1000000 && $n < 1000000000)
{
if($n%1000000 === 0)
{
$formatted = ($n/1000000);
}
else
{
$formatted = substr($n, 0, -6).'.'.substr($n, -6, 1);
if(substr($formatted, -1, 1) === '0')
{
$formatted = substr($formatted, 0, -2);
}
}
$formatted.= 'M';
} else
if($n >= 1000000000 && $n < 1000000000000)
{
if($n%1000000000 === 0)
{
$formatted = ($n/1000000000);
}
else
{
$formatted = substr($n, 0, -9).'.'.substr($n, -9, 1);
if(substr($formatted, -1, 1) === '0')
{
$formatted = substr($formatted, 0, -2);
}
}
$formatted.= 'G';
} else
if($n >= 0 && $n < 1000)
{
$formatted= $n;
}
return $formatted;
}