PHP Count round thousand to a K style count like facebook Share . . . Twitter Button ect

后端 未结 12 1799
北恋
北恋 2020-12-13 05:15

Ok so I am trying to turn my hit counter to round thousands to a single digit too display 3 thousand hits as 3K for example, like the Facebook Share and Twitter Tweet Button

相关标签:
12条回答
  • 2020-12-13 05:33

    Here comes a PHP function to format numbers to nearest thousands such as Kilos, Millions, Billions, and Trillions with comma

    Function

    function thousandsCurrencyFormat($num) {
    
      if($num>1000) {
    
            $x = round($num);
            $x_number_format = number_format($x);
            $x_array = explode(',', $x_number_format);
            $x_parts = array('k', 'm', 'b', 't');
            $x_count_parts = count($x_array) - 1;
            $x_display = $x;
            $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
            $x_display .= $x_parts[$x_count_parts - 1];
    
            return $x_display;
    
      }
    
      return $num;
    }
    

    Output

    thousandsCurrencyFormat(3000) - 3k
    thousandsCurrencyFormat(35500) - 35.5k
    thousandsCurrencyFormat(905000) - 905k
    thousandsCurrencyFormat(5500000) - 5.5m
    thousandsCurrencyFormat(88800000) - 88.8m
    thousandsCurrencyFormat(745000000) - 745m
    thousandsCurrencyFormat(2000000000) - 2b
    thousandsCurrencyFormat(22200000000) - 22.2b
    thousandsCurrencyFormat(1000000000000) - 1t (1 trillion)
    

    Resources

    https://code.recuweb.com/2018/php-format-numbers-to-nearest-thousands/

    0 讨论(0)
  • 2020-12-13 05:36

    Question is 8 years old but each time I see an answer that contains an else statement, I think it can be done in a better (cleaner) way.

    <?php
    
    if (!function_exists('format_number_in_k_notation')) {
        function format_number_in_k_notation(int $number): string
        {
            $suffixByNumber = function () use ($number) {
                if ($number < 1000) {
                    return sprintf('%d', $number);
                }
    
                if ($number < 1000000) {
                    return sprintf('%d%s', floor($number / 1000), 'K+');
                }
    
                if ($number >= 1000000 && $number < 1000000000) {
                    return sprintf('%d%s', floor($number / 1000000), 'M+');
                }
    
                if ($number >= 1000000000 && $number < 1000000000000) {
                    return sprintf('%d%s', floor($number / 1000000000), 'B+');
                }
    
                return sprintf('%d%s', floor($number / 1000000000000), 'T+');
            };
    
            return $suffixByNumber();
        }
    }
    
    dump(format_number_in_k_notation(123)); // "123"
    dump(format_number_in_k_notation(73000)); // "73K+"
    dump(format_number_in_k_notation(216000)); // "216K+"
    dump(format_number_in_k_notation(50400123)); // "50M+"
    dump(format_number_in_k_notation(12213500100600)); // "12T+"
    
    die;
    
    0 讨论(0)
  • 2020-12-13 05:41

    Rounding up, not accounting for any abbreviations above 'k' or thousands, showing one decimal place.

    function kConverter($number) {
        if ($number >= 1000) {
            return number_format(($number / 1000), 1) . 'k';
        } else {
            return $number;
        }
    }
    
    kConverter(1)          = 1
    kConverter(111)        = 111
    kConverter(999)        = 999
    kConverter(1000)       = "1.0k"
    kConverter(1499)       = "1.5k"
    kConverter(1500)       = "1.5k"
    kConverter(1501)       = "1.5k"
    kConverter(1550)       = "1.6k"
    kConverter(11501)      = "11.5k"
    kConverter(1000000000) = "1,000,000.0k"
    kConverter(1234567890) = "1,234,567.9k"
    
    0 讨论(0)
  • 2020-12-13 05:41

    My func

    function numsize($size,$round=2){
        $unit=['', 'K', 'M', 'B', 'T'];
        return round($size/pow(1000,($i=floor(log($size,1000)))),$round).$unit[$i];
    }
    
    0 讨论(0)
  • 2020-12-13 05:42

    This is a modified version with k and m lowercase and show one decimal place for milllions.

    <?php
    
        if ($value > 999 && $value <= 999999) {
        $result  = floor($value / 1000) . 'k';
        } elseif ($value > 999999) {
        $result  = number_format((float)$value , 1, '.', '')/1000000 . 'm';
        } else {
        $result  = $value;
        }
    ?>
    
    0 讨论(0)
  • 2020-12-13 05:44

    Use floor instead of round if you want 3500 to round down to 3 K.

    Otherwise, your code works, albeit problematically. Try this:

    if ($postresultscount > 1000) {
      $result = floor($postresultscount / 1000) . 'K';
    } else {
      $result = $postresultscount;
    }
    
    echo 'document.write("' . $result . '")";
    

    It also appears you're writing JavaScript using PHP—take care.

    0 讨论(0)
提交回复
热议问题