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

0 votes

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

"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

- 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).

0 votes

```
$number="6000";
$val=($number/1000)."k";
//= 6k
```

or if the `$number="6k";`

```
echo str_replace("k","000",$number);
```

0 votes

*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

'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

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

```
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
}
```

...