What does "6k views" mean and how can I format this number in PHP?
-
1*(related)* http://en.wikipedia.org/wiki/SI_prefix – Gordon Apr 24 '10 at 09:44
7 Answers
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;
}

- 643,351
- 109
- 780
- 844
-
Actually it gives `1k 10k 100k 500M 500M 50000M 333333G` for `array(1000, 10000, 100000, 1000000, 1000000, 100000000, 1000000000);` – Gordon Apr 24 '10 at 10:19
'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.

- 3,129
- 23
- 11
-
Thanks, don't forget Gumbo's answer though. It's clearly more generic, if a little more cryptic for the uninitiated. – Geoff Apr 24 '10 at 09:27
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

- 312,688
- 75
- 539
- 559
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 :)

- 377,238
- 77
- 533
- 578
-
-
@moustafa: hmmm :) meter and gram are different units, just concentrate on **kilo** part. – Sarfraz Apr 24 '10 at 09:20
-
-
@moustafa, just kilo. In this case would be kilo - views => 6,000 views – OscarRyz Apr 24 '10 at 09:21
- k means 1000, so '6k views' = 6000 views.
- 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.
- after you queried the value, divide it by 1000 and add a 'k' to the number (if the number is > 1000).

- 60,705
- 7
- 138
- 176
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
}

- 1
- 1