1

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;

}
XeanNeia
  • 13
  • 1
  • 5
  • 5
    Don't use [round()](http://www.php.net/manual/en/function.round.php) then, use [floor()](http://www.php.net/manual/en/function.floor.php)... if you don't understand what they do, don't use them until you've read the PHP docs and do understand them – Mark Baker Oct 31 '14 at 14:37
  • Round function rounds the number, what you want is a floor() function instead – user2209644 Oct 31 '14 at 14:38
  • Hi, just wanted to let you know that I did not know you mentioned me in your post. You should leave an actual comment next time and use `@MonkeyZeus`. Anyways, I see your update and your code looks good but I realize that I am missing a piece of code so please see my edit. – MonkeyZeus Oct 31 '14 at 18:31

2 Answers2

2

You can use this to calculate thousands. You can use this formula to figure out the formula for millions as well.

$n = 2000;
$formatted = '';

if($n >= 1000 && $n < 1000000)
{
    if($n%1000 === 0)
    {
        $formatted = ($n/1000);
    }
    else
    {
        $formatted = substr($n, 0, -3).'.'.substr($n, -3, -2);

        if(substr($formatted, -1, 1) === '0')
        {
            $formatted = substr($formatted, 0, -2);
        }
    }

    $formatted.= 'k';
}

echo $formatted;

Also, please use curly braces, ALWAYS. Future you will thank present you.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • In case that `$n` is smaller than 1000 or bigger than 999999, your code won't `echo` anything but an empty string. – prehfeldt Oct 31 '14 at 14:53
  • @prehfeldt Thank you. What part of `You can use this to calculate thousands.` made you think that this would calculate anything other than thousands? – MonkeyZeus Oct 31 '14 at 15:43
  • @XeanNeia I see your edit and it looks good. Have you tried running it through some tests? I've been using an online PHP compiler so I definitely recommend trying it out. http://writecodeonline.com/php/ – MonkeyZeus Oct 31 '14 at 19:22
  • @MonkeyZeus Thank you very much,, I have tested this code on writecodeonline.com/php ..and it worked like a charm. thanks again.. – XeanNeia Oct 31 '14 at 19:41
  • @XeanNeia you're welcome. I do see another improvement though. You can change `$formatted = substr($n, 0, -3).'.'.substr($n, -3, -2);` into `$formatted = substr($n, 0, -3).'.'.substr($n, -3, 1);` and use -6,1 for mega and -9,1 for giga. Glad to hear it's working for you! – MonkeyZeus Oct 31 '14 at 20:29
  • @MonkeyZeus Thanks again... plz see my edit... any other improvement required? – XeanNeia Nov 01 '14 at 14:07
  • @XeanNeia Looks fine to me, I just hope you're testing it for all edge-case scenarios. – MonkeyZeus Nov 01 '14 at 20:09
  • @MonkeyZeus Yeah I have tested... OK now itz done... Thanks alot :) – XeanNeia Nov 02 '14 at 17:04
0

What about number greater than 10000

function facebookFormattter($digit) {
    if ($digit >= 1000000000) {
        return round($digit/ 1000000000, 1). 'G';
    }
    if ($digit >= 1000000) {
        return round($digit/ 1000000, 1).'M';
    }
    if ($digit >= 1000) {
        return round($digit/ 1000, 1). 'K';
    }
    return $digit;
}
Notepad
  • 1,659
  • 1
  • 12
  • 14