PHP统计数千到K样式计数,如Facebook分享。 。 。 Twitter按钮等

时间:2010-11-07 04:34:58

标签: php

好吧所以我试图将我的点击计数器变成数千到一位数也显示3千次点击为3K,例如Facebook Share和Twitter Tweet Buttons。这是我的代码。知道我做错了吗?

$postresultscount = (($resultscount) ? $resultscount->sumCount : 1);
$k = 1000;
$L = '';
if ($postresultscount > $k) {
    $echoxcount = round($postresultscount/$k);
    $L = 'K';
} else if ($postresultscount == $k) {
    $echoxcount = 1;
    $L = 'K';
} else {
    $echoxcount = $postresultscount;
}

echo 'document.write("'.$echoxcount.' '.$L.'")';

14 个答案:

答案 0 :(得分:23)

这里有一个PHP函数,用于将数字格式化为最接近的千位数,例如Kilos,Millions,Billions和Trillions 逗号

<强>功能

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;
}

<强>输出

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)

<强>资源

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

答案 1 :(得分:12)

Yuki

更好一点
if ($value > 999 && $value <= 999999) {
    $result = floor($value / 1000) . ' K';
} elseif ($value > 999999) {
    $result = floor($value / 1000000) . ' M';
} else {
    $result = $value;
}

答案 2 :(得分:3)

如果您希望3500将向下舍入到floor,请使用round代替3 K

否则,您的代码会起作用,尽管有问题。试试这个:

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

echo 'document.write("' . $result . '")";

您似乎也在使用PHP-taking care编写JavaScript。

答案 3 :(得分:3)

function shortNumber($num) 
{
    $units = ['', 'K', 'M', 'B', 'T'];
    for ($i = 0; $num >= 1000; $i++) {
        $num /= 1000;
    }
    return round($num, 1) . $units[$i];
}

我修改了一个函数,该函数是由bashy创建的,以人类可读的形式显示字节:

https://laracasts.com/discuss/channels/laravel/human-readable-file-size-and-time

答案 4 :(得分:3)

问题已经有8年了,但是每次我看到包含else陈述的答案时,我都认为可以在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;

答案 5 :(得分:2)

这是一个k和m小写的修改版本,显示了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;
    }
?>

答案 6 :(得分:0)

if ($postresultscount > 999999) {
    $postresultscount = floor($postresultscount / 1000000) . ' M';
}
elseif ($postresultscount > 999) {
    $postresultscount = floor($postresultscount / 1000) . ' K';
}
echo $postresultscount;

答案 7 :(得分:0)

function print_number_count($number) {
    $units = array( '', 'K', 'M', 'B');
    $power = $number > 0 ? floor(log($number, 1000)) : 0;
    if($power > 0)
        return @number_format($number / pow(1000, $power), 2, ',', ' ').' '.$units[$power];
    else
        return @number_format($number / pow(1000, $power), 0, '', '');
}

答案 8 :(得分:0)

四舍五入,不考虑'k'或数千以上的缩写,显示小数点后一位。

function numToKs($number) {
    if ($number >= 1000) {
        return number_format(($number / 1000), 1) . 'k';
    } else {
        return $number;
    }
}
numToKs(1)          = 1
numToKs(111)        = 111
numToKs(999)        = 999
numToKs(1000)       = "1.0k"
numToKs(1499)       = "1.5k"
numToKs(1500)       = "1.5k"
numToKs(1501)       = "1.5k"
numToKs(1550)       = "1.6k"
numToKs(11501)      = "11.5k"
numToKs(1000000000) = "1,000,000.0k"
numToKs(1234567890) = "1,234,567.9k"

答案 9 :(得分:0)

此问题的目的与此处Shorten long numbers to K/M/B?

中的问题相同

参考: https://gist.github.com/RadGH/84edff0cc81e6326029c

尝试以下代码:

function number_format_short( $n, $precision = 1 ) {
    if ($n < 900) {
        // 0 - 900
        $n_format = number_format($n, $precision);
        $suffix = '';
    } else if ($n < 900000) {
        // 0.9k-850k
        $n_format = number_format($n / 1000, $precision);
        $suffix = 'K';
    } else if ($n < 900000000) {
        // 0.9m-850m
        $n_format = number_format($n / 1000000, $precision);
        $suffix = 'M';
    } else if ($n < 900000000000) {
        // 0.9b-850b
        $n_format = number_format($n / 1000000000, $precision);
        $suffix = 'B';
    } else {
        // 0.9t+
        $n_format = number_format($n / 1000000000000, $precision);
        $suffix = 'T';
    }
  // Remove unecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1"
  // Intentionally does not affect partials, eg "1.50" -> "1.50"
    if ( $precision > 0 ) {
        $dotzero = '.' . str_repeat( '0', $precision );
        $n_format = str_replace( $dotzero, '', $n_format );
    }
    return $n_format . $suffix;
}

上面的代码创建了一个转换数字的函数。要稍后使用此功能,只需在下面的代码中调用它即可:

// Example Usage:
number_format_short(7201); // Output: 7.2k

答案 10 :(得分:0)

四舍五入,不考虑'k'或数千以上的缩写,显示小数点后一位。

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"

答案 11 :(得分:0)

我的功能

let image = yourImage.withTintColor(.systemRed)

答案 12 :(得分:0)

对于这个特别古老的问题已经给出了几个很好的答案,但是,大多数对我的口味来说太简单了或者不容易扩展到更多的单位,所以我使用的是:

# The function that returns a number formatted as a string in thousands, millions etc.
public static function getNumberAbbreviation (Int $number, Int $decimals = 1) : String {
    # Define the unit size and supported units.
    $unitSize = 1000;
    $units = ["", "K", "M", "B", "T"];

    # Calculate the number of units as the logarithm of the absolute value with the
    # unit size as base.
    $unitsCount = ($number === 0) ? 0 : floor(log(abs($number), $unitSize));

    # Decide the unit to be used based on the counter.
    $unit = $units[min($unitsCount, count($units) - 1)];

    # Divide the value by unit size in the power of the counter and round it to keep 
    # at most the given number of decimal digits.
    $value = round($number / pow($unitSize, $unitsCount), $decimals);

    # Assemble and return the string.
    return $value . $unit;
}

答案 13 :(得分:0)

我在 Twitter 的启发下创建了自己的方法。

功能:

function legibleNumb($numb, $lang = 'en') {
    if ($lang == 'tr') { // Usage with commas in Turkish
        if ($numb >= 1000000) { // Million
            if (strstr(round(number_format($numb,0,',','.'),1),'.')) {
                $legibleNumb = number_format(round(number_format($numb,0,',','.'),1),1,',','.') . ' Mn';
            } else {
                $legibleNumb = round(number_format($numb,0,',','.'),1) . ' Mn';
            }
        } elseif ($numb >= 100000 && $numb < 1000000) { // One hundred thousand
            $legibleNumb = round(number_format($numb,0,',','.'),0) . ' B';
        } elseif ($numb >= 10000 && $numb < 100000) { // Ten thousand
            if (strstr(round(number_format($numb,0,',','.'),1),'.')) {
                $legibleNumb = number_format(round(number_format($numb,0,',','.'),1),1,',','.') . ' B';
            } else {
                $legibleNumb = round(number_format($numb,0,',','.'),1) . ' B';
            }
        } else {
            $legibleNumb = number_format($numb,0,',','.');
        }
    } else { // Dotted usage in English
        if ($numb >= 1000000) { // Million
            $legibleNumb = round(number_format($numb,0,',','.'),1) . ' M';
        } elseif ($numb >= 100000 && $numb < 1000000) { // One hundred thousand
            $legibleNumb = round(number_format($numb,0,',','.'),0) . ' K';
        } elseif ($numb >= 10000 && $numb < 100000) { // Ten thousand
            $legibleNumb = round(number_format($numb,0,',','.'),1) . ' K';
        } else {
            $legibleNumb = number_format($numb,0,',','.');
        }
    }
    return $legibleNumb;
}

用法:

echo legibleNumb(9999999,'en');
echo legibleNumb(9999999,'tr');

echo legibleNumb(54669,'en');
echo legibleNumb(54669,'tr');

echo legibleNumb(5466,'en');
echo legibleNumb(5466,'tr');

结果:

10 M
10 Mn

54.7 K
54,7 B

5.466
5.466

您可以在这里尝试并查看示例用法:https://glot.io/snippets/eljyd9ssjx