0

I have my custom plugin for set and get post view count on wordpress. My function give me post view count like 3833056 or 19834 or 785923873.

I can format this counts with number_format() function but its give me 3833056 => 3,833,056

I want to format this post view counts like (3833056 => 3.8 million) or (19834 => 19.8 thousand) or (1284 => 1.2 thousand)

How can i do this with php

Thanks

Kadir Çetintaş
  • 207
  • 1
  • 5
  • 13
  • Possible duplicate of [Convert number into xx.xx million format?](http://stackoverflow.com/questions/10221694/convert-number-into-xx-xx-million-format) – MX D Nov 13 '15 at 08:19

2 Answers2

3

You can't really do that right out of the box.

There's a neat function that will help you achieve what you want:

<?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);
    }
?>

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

Andrius
  • 5,934
  • 20
  • 28
0

Have a look at: coduo/php-humanizer the metric suffix part of the readme should fit your requirements.

nihylum
  • 542
  • 3
  • 7