php - Abbreviate Number Function Won't Output Negative Value -


so have class, appreciates numbers. example, abbreviatenum::convert(1178); round , turn 1.18k.

this works should, nicely. however, can't seem figure out how output negative numbers. if run abbreviatenum::convert(-1178);, output same response 1.18k. without negative indicator.

any tips on how fix this?

<?php  namespace app\helpers;  class abbreviatenum {      /**      * abbreviate long numbers      *       * @return response      */     public static function convert($num)     {         $num = preg_replace('/[^0-9]/', '', $num);         $sizes = array("", "k", "m");          if ($num == 0) return(0);          else return (round($num/pow(1000, ($i = floor(log($num, 1000)))), 2) . $sizes[$i]);     }  } 

here modified function provides little bit more robustness strings accept.

public static function convert($num) {     $num = intval(preg_replace('/[^\-\.0-9]/', '', $num));     $sizes = array("", "k", "m");      if ($num == 0) return(0);      else return (round($num/pow(1000, ($i = floor(log(abs($num), 1000)))), 2) . $sizes[abs($i)]); } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -