如何将1000的页面浏览量四舍五入为1k,依此类推[重复]

我对php和mysql的了解很少。我的网站显示特定图像的观看次数,但我希望视图以四舍五入的形式显示。这是我在网站上查看时所使用的代码

<div class="pic rounded-8" style="overflow:hidden;"><div style="margin:0 0 36px 0;overflow:hidden;border:none;height:auto;"><img src='<?php echo "./images/".$cur_image['filename'].".".$cur_image['ext']; 
$viewed = $cur_image['id'];
mysql_query("UPDATE `images` SET `views` = `views`+1 WHERE `id`='$viewed'") or die("Couldnt update");

?>' style="max-width:590px;"></div></div>
<span style="font-style:italic;">Views: <?php echo $cur_image['views']; ?></span>

我尝试为$ views或$ viewed创建用于视图的函数,但是它不起作用。

有人可以指导我如何实现这一目标,并以1个小数位显示视图(例如1.2k或1.2m)

评论
  • 凹凸曼
    凹凸曼 回复

    我从堆栈溢出中发现的内容(没有原始链接)修改了一个很酷的代码段。

    function formatFileSize($size)
    {
        $output = '';
    
        $number = 0;
        $count = 0;
    
        // Determine the magnitude.
        $holder = (int)($size/KB);
        while( $holder >= 1)
        {
          $number = $holder;
          $count++;
    
          $holder = (int)($holder/KB);
        }
        $output .= $number;
        if ($count > 0)
        {
          $output .= '.' . substr($holder%(KB*$count), 0, 2);
        }
    
        // Give appropriate suffix.
        switch ($count)
        {
          case 0:
            $output .= 'bytes';
            break;
          case 1:
            $output .= 'KB';
            break;
          case 2:
            $output .= 'MB';
            break;
          case 3:
            $output .= 'GB';
            break;
          case 4:
            $output .= 'TB';
            break;
          case 5:
            $output .= 'PB';
            break;
        }
    
        return $output;
    }
    

    Just replace KB constant with 1000. And I think this snippet does 2 decimal places. Should at least get you in the right direction.