-5

I need of a function in PHP to format numbers like Facebook "likes".

Example:

12345 = 12,3 K

123456 = 123 K

1234567 = 1,23 M

Thank you!!

  • Please read [what have you tried](http://whathaveyoutried.com)? Also, understand that SO is for help and not "give me"s or "I need"s – UnholyRanger Mar 04 '13 at 14:57
  • I know this has been asked here before but having issues finding it. – John Conde Mar 04 '13 at 14:58
  • something like this? [How to format numbers in php 1,000 to 1k](http://stackoverflow.com/questions/4703469/how-to-format-numbers-in-php-1-000-to-1k) – UnholyRanger Mar 04 '13 at 15:01

1 Answers1

4

Write a function which do this!?

function format_num($n) {
    $s = array("K", "M", "G", "T");
    $out = "";
    while ($n >= 1000 && count($s) > 0) {
        $n = $n / 1000.0;
        $out = array_shift($s);
    }
    return round($n, max(0, 3 - strlen((int)$n))) ." $out";
}
Philipp
  • 15,377
  • 4
  • 35
  • 52