fork(1) download
  1. <?php
  2.  
  3. class NumberQuantifier {
  4. protected $quantifierList;
  5.  
  6. public function __construct($quantifierList) {
  7. $this->quantifierList = $quantifierList;
  8. arsort($this->quantifierList);
  9. }
  10.  
  11. public function quantify($number) {
  12. foreach ($this->quantifierList as $symbol => $threshold) {
  13. if ($threshold > $number) continue;
  14.  
  15. return number_format($number / $threshold, 1) . $symbol;
  16. }
  17. }
  18. }
  19.  
  20. $numberQuantifier = new NumberQuantifier(array(
  21. 'M' => 1000000,
  22. 'B' => 1000000000,
  23. 'K' => 1000
  24. ));
  25.  
  26. echo $numberQuantifier->quantify(148293);
  27. echo $numberQuantifier->quantify(2356458);
  28. echo $numberQuantifier->quantify(23568534);
  29. echo $numberQuantifier->quantify(8927492842);
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
148.3K2.4M23.6M8.9B