post 观看次数的数字格式

Number format for post view count

我有自己的自定义插件,可以在 wordpress 上设置和获取 post 查看次数。 我的功能给我 post 观看次数,例如 3833056 或 19834 或 785923873。

我可以用 number_format() 函数格式化这个计数,但它给我 3833056 => 3,833,056

我想将此 post 观看次数格式化为 (3833056 => 380 万) 或 (19834 => 19800) 或 (1284 => 1200)

我如何用 php

做到这一点

谢谢

你真的不能开箱即用。

有一个巧妙的功能可以帮助您实现您想要的:

<?php
    #    Output easy-to-read numbers
    #    by james at bandit.co.nz
    function bd_nice_number($n) {
        // first strip any formatting;
        $n = (0+str_replace(",","",$n));

        // is this a number?
        if(!is_numeric($n)) return false;

        // now filter it;
        if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
        else if($n>1000000000) return round(($n/1000000000),1).' billion';
        else if($n>1000000) return round(($n/1000000),1).' million';
        else if($n>1000) return round(($n/1000),1).' thousand';

        return number_format($n);
    }
?>

来源:http://php.net/manual/en/function.number-format.php#89888

查看:coduo/php-humanizer 自述文件的公制后缀部分应该符合您的要求。