What Does '6k views' mean and how can I format the number in PHP

0 votes
asked Apr 24, 2010 by unix-man

What does "6k views" mean and how can I format this number in PHP?

8 Answers

0 votes
answered Jan 24, 2010 by sarfraz

In 6k, the k means kilo (i hope you know) which equals to 6000. You replace the thousand figure with k, that's it. Hope that helps :)

0 votes
answered Jan 24, 2010 by andy-e

"6k views" means 6 kilo-views, or 6,000 views. You could extract this number in PHP using intval and multiplying by 1,000:

$val = intval("6k views") * 1000; // = 6000
0 votes
answered Jan 24, 2010 by femaref
  1. k means 1000, so '6k views' = 6000 views.
  2. normally, on every access to a page, a counter in a database is increased by 1. This just gets queried on every page access and printed.
  3. after you queried the value, divide it by 1000 and add a 'k' to the number (if the number is > 1000).
0 votes
answered Jan 24, 2010 by cosy
$number="6000";
$val=($number/1000)."k";
//= 6k

or if the $number="6k";

echo str_replace("k","000",$number);
0 votes
answered Apr 24, 2010 by gumbo

k is the abbreviation for the Kilo prefix and means thousand. So 6k means six thousand.

You can format a number in such a way with the following function using division:

function format($number) {
    $prefixes = 'kMGTPEZY';
    if ($number >= 1000) {
        for ($i=-1; $number>=1000; ++$i) {
            $number /= 1000;
        }
        return floor($number).$prefixes[$i];
    }
    return $number;
}

Or using logarithm base 10 and exponentiation:

function format($number) {
    $prefixes = 'kMGTPEZY';
    if ($number >= 1000) {
        $log1000 = floor(log10($number)/3);
        return floor($number/pow(1000, $log1000)).$prefixes[$log1000-1];
    }
    return $number;
}
0 votes
answered Apr 24, 2010 by geoff

'6k views' on StackOverflow refers to the number of views a question has received. It means 6000 views.

If you're looking to format a similar style number in php then try something like

$number = "";
if( $value > 1000 )
{
    $number .= floor($value / 1000) . "k";
} else {
    $number .= $value;
}
echo $number . " views".

Obviously you can add cases for m, g and t views if desired.

0 votes
answered Apr 24, 2010 by gordon

Abridged from http://tamlyn.org/2008/12/formatting-bytes-with-significant-figures-in-php/

/** Calculate $value to $sigFigs significant figures */
function sigFig($value, $sigFigs = 3) {
    //convert to scientific notation e.g. 12345 -> 1.2345x10^4
    //where $significand is 1.2345 and $exponent is 4
    $exponent = floor(log10(abs($value))+1);
    $significand = round(($value
        / pow(10, $exponent))
        * pow(10, $sigFigs))
        / pow(10, $sigFigs);
    return $significand * pow(10, $exponent);
}

/** Format $value with the appropriate SI prefix symbol */
function format($value, $sigFigs = 3)
{
    //SI prefix symbols
    $units = array('', 'k', 'M', 'G', 'T', 'P', 'E');
    //how many powers of 1000 in the value?
    $index = floor(log10($value)/3);
    $value = $index ? $value/pow(1000, $index) : $value;
    return sigFig($value, $sigFigs) . $units[$index];
}

Doing *11 because *10 is too obvious

for($number = 100; $number < 100000000000000000000; $number*=11) {
   echo format($number), PHP_EOL;
}

gives

100 1.1k 12.1k 133k 1.46M 16.1M 177M 1.95G 21.4G 236G 2.59T 28.5T 314T 3.45P 38P 418P 4.59E 50.5E 

If you need the decimals, use the above, else Gumbo's solution is more compact. Gives:

100 1k 12k 133k 1M 16M 177M 1G 21G 235G 2T 28T 313T 3P 37P 417P 4E 50E
0 votes
answered Jan 25, 2016 by alejandro-arias
function sigFig($value, $sigFigs = 3) {
    setlocale(LC_ALL, 'it_IT@euro', 'it_IT', 'it'); 
    $exponent = floor(log10(abs($value))+1);
    $significand = round(($value
        / pow(10, $exponent))
        * pow(10, $sigFigs))
        / pow(10, $sigFigs);
    return $significand * pow(10, $exponent);
}

function format($value, $sigFigs = 3)
{
    $numero = $value;

    if ($numero > 9999) {

      $units = array('', 'k', 'M', 'G', 'T', 'P', 'E');

      $index = floor(log10($value)/3);
      $value = $index ? $value/pow(1000, $index) : $value;
      return sigFig($value, $sigFigs) . $units[$index];
    }else{
      return number_format($numero, 0, '', '.'); ;
    }

    //Resultados:
      //9999 -> 9.999 views
      //10000 -> 10k views
      //10200 -> 10,2k views
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...