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


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.