Shorten long numbers to K/M/B?

前端 未结 10 1869
南方客
南方客 2020-11-27 03:39

I\'ve googled this a lot but i can\'t find any helpful functions based on my queries.

What i want is:

100 -> 100
1000 -> 1,000
142840 -> 142         


        
相关标签:
10条回答
  • 2020-11-27 04:03

    I took the answer BoltClock provided and tweaked it a bit with ease of configuration in mind.

    // Shortens a number and attaches K, M, B, etc. accordingly
    function number_shorten($number, $precision = 3, $divisors = null) {
    
        // Setup default $divisors if not provided
        if (!isset($divisors)) {
            $divisors = array(
                pow(1000, 0) => '', // 1000^0 == 1
                pow(1000, 1) => 'K', // Thousand
                pow(1000, 2) => 'M', // Million
                pow(1000, 3) => 'B', // Billion
                pow(1000, 4) => 'T', // Trillion
                pow(1000, 5) => 'Qa', // Quadrillion
                pow(1000, 6) => 'Qi', // Quintillion
            );    
        }
    
        // Loop through each $divisor and find the
        // lowest amount that matches
        foreach ($divisors as $divisor => $shorthand) {
            if (abs($number) < ($divisor * 1000)) {
                // We found a match!
                break;
            }
        }
    
        // We found our match, or there were no matches.
        // Either way, use the last defined value for $divisor.
        return number_format($number / $divisor, $precision) . $shorthand;
    }
    
    0 讨论(0)
  • 2020-11-27 04:03
    function number_abbr($number)
    {
        $abbrevs = [12 => 'T', 9 => 'B', 6 => 'M', 3 => 'K', 0 => ''];
    
        foreach ($abbrevs as $exponent => $abbrev) {
            if (abs($number) >= pow(10, $exponent)) {
                $display = $number / pow(10, $exponent);
                $decimals = ($exponent >= 3 && round($display) < 100) ? 1 : 0;
                $number = number_format($display, $decimals).$abbrev;
                break;
            }
        }
    
        return $number;
    }
    

    Works for positives and negatives.

    0 讨论(0)
  • 2020-11-27 04:05

    Altough this question was asked quite some time ago I have a different solution, which I think is even more simple:

    function shorten($number){
        $suffix = ["", "K", "M", "B"];
        $precision = 1;
        for($i = 0; $i < count($suffix); $i++){
            $divide = $number / pow(1000, $i);
            if($divide < 1000){
                return round($divide, $precision).$suffix[$i];
                break;
            }
        }
    }
    echo shorten(1000);
    

    I hope that it's still helpful for someone.

    0 讨论(0)
  • 2020-11-27 04:11

    I took a different approach based on the previous solutions. Basically it uses the log() function to get rid of the for statement:

    function number_shorten($number, $precision = 3)
    {
        $suffixes = ['', 'K', 'M', 'B', 'T', 'Qa', 'Qi'];
        $index = (int) log(abs($number), 1000);
        $index = max(0, min(count($suffixes) - 1, $index)); // Clamps to a valid suffixes' index
        return number_format($number / 1000 ** $index, $precision) . $suffixes[$index];
    }
    
    0 讨论(0)
  • 2020-11-27 04:12

    You can try this

     function number_formation($number, $precision = 3) {
            if ($number < 1000000) {
    
                $formatted_number = number_format($number); /* less than a million */
            } else if ($number < 1000000000) {
    
                $formatted_number = number_format($number / 1000000, $precision) . 'M'; /* billion */
            } else {
    
                $formatted_number = number_format($number  / 1000000000, $precision) . 'B'; /* for billion */
            }
    
            return $formatted_number;
        }
    
    0 讨论(0)
  • 2020-11-27 04:15

    Use number_format():

    if ($n < 1000000) {
        // Anything less than a million
        $n_format = number_format($n);
    } else if ($n < 1000000000) {
        // Anything less than a billion
        $n_format = number_format($n / 1000000, 3) . 'M';
    } else {
        // At least a billion
        $n_format = number_format($n / 1000000000, 3) . 'B';
    }
    

    I would totally appreciate any custom functions to dynamically choose the limit if possible.

    If "limit" refers to the number of decimal places (the precision), that's easy:

    function custom_number_format($n, $precision = 3) {
        if ($n < 1000000) {
            // Anything less than a million
            $n_format = number_format($n);
        } else if ($n < 1000000000) {
            // Anything less than a billion
            $n_format = number_format($n / 1000000, $precision) . 'M';
        } else {
            // At least a billion
            $n_format = number_format($n / 1000000000, $precision) . 'B';
        }
    
        return $n_format;
    }
    
    0 讨论(0)
提交回复
热议问题