What Does '6k views' mean and how can I fo

2020-02-20 05:27发布

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

标签: php
7条回答
啃猪蹄的小仙女
2楼-- · 2020-02-20 05:29
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
}
查看更多
一夜七次
3楼-- · 2020-02-20 05:32

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

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-20 05:39
$number="6000";
$val=($number/1000)."k";
//= 6k

or if the $number="6k";

echo str_replace("k","000",$number);
查看更多
smile是对你的礼貌
5楼-- · 2020-02-20 05:47

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

查看更多
仙女界的扛把子
6楼-- · 2020-02-20 05:51

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;
}
查看更多
▲ chillily
7楼-- · 2020-02-20 05:52
  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).
查看更多
登录 后发表回答