Posted By

Jamie on 08/24/10


Tagged

numbers round


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hugomastromauro


Round Large Numbers to a Letter Abbreviation eg: 25,000 to 25K


 / Published in: PHP
 

URL: http://us3.php.net/manual/en/function.filesize.php#99333

A little restrictive, but should work for simple implementations. I\'m using this to replicate Facebook\'s \"35K people like this\" button format.\r\nThe original code is included below my modification.

  1. function abbr_number($size) {
  2. $size = preg_replace('/[^0-9]/','',$size);
  3. $sizes = array("", "K", "M");
  4. if ($size == 0) { return('n/a'); } else {
  5. return (round($size/pow(1000, ($i = floor(log($size, 1000)))), 0) . $sizes[$i]); }
  6. }
  7.  
  8. function format_size($size) {
  9. $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  10. if ($size == 0) { return('n/a'); } else {
  11. return (round($size/pow(1024, ($i = floor(log($size, 1024)))), $i > 1 ? 2 : 0) . $sizes[$i]); }
  12. }

Report this snippet  

You need to login to post a comment.