Tero

[+] Format Number with Suffix

Nov 2nd, 2016
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. <?php
  2.  function format($n, $v = 1 ) {
  3.    if ($n < 900) {
  4.     $format = number_format($n, $v);
  5.     $forma = '';
  6.    } else if ($n < 900000) {        
  7.     $format = number_format($n / 1000, $v);
  8.     $forma = 'k';
  9.    } else if ($n < 900000000) {
  10.     $format = number_format($n / 1000000, $v);
  11.     $forma = 'M';
  12.    } else if ($n < 900000000000) {
  13.     $format = number_format($n / 1000000000, $v);
  14.     $forma = 'B';
  15.    } else {
  16.     $format = number_format($n / 1000000000000, $v);
  17.     $forma = 'T';
  18.    }
  19.    if ($v > 0) {
  20.     $script = '.' . str_repeat( '0', $v );
  21.     $format = str_replace( $script, '', $format );
  22.    }
  23.  return $format . $forma;
  24.  }
  25.  
  26. #Usage
  27. $number = '10000';
  28. echo format($number); / return 10k
  29. ?>
Advertisement
Add Comment
Please, Sign In to add comment