好的,所以我试图将我的点击计数器变成数千到一位数也显示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.'")';
Run Code Online (Sandbox Code Playgroud)
Raf*_*shi 27
这里有一个PHP函数将数字格式化为最接近的数字,例如Kilos,Millions,Billions和Trillions with逗号
功能
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;
}
Run Code Online (Sandbox Code Playgroud)
产量
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)
Run Code Online (Sandbox Code Playgroud)
资源
https://code.recuweb.com/2018/php-format-numbers-to-nearest-thousands/
Jus*_*ent 13
function shortNumber($num)
{
$units = ['', 'K', 'M', 'B', 'T'];
for ($i = 0; $num >= 1000; $i++) {
$num /= 1000;
}
return round($num, 1) . $units[$i];
}
Run Code Online (Sandbox Code Playgroud)
我从bashy这里创建的一个函数中改编了这个,以人类可读的形式显示字节:
https://laracasts.com/discuss/channels/laravel/human-readable-file-size-and-time
Gia*_*nFS 12
比Yuki的帖子好一点
if ($value > 999 && $value <= 999999) {
$result = floor($value / 1000) . ' K';
} elseif ($value > 999999) {
$result = floor($value / 1000000) . ' M';
} else {
$result = $value;
}
Run Code Online (Sandbox Code Playgroud)
问题已有 8 年历史,但每次我看到包含else声明的答案时,我都认为可以用更好(更干净)的方式来完成。
<?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;
Run Code Online (Sandbox Code Playgroud)
如果您希望 3500 向下舍入为,请使用floor而不是。round3 K
否则,你的代码可以工作,尽管有问题。尝试这个:
\n\nif ($postresultscount > 1000) {\n $result = floor($postresultscount / 1000) . \'K\';\n} else {\n $result = $postresultscount;\n}\n\necho \'document.write("\' . $result . \'")";\nRun Code Online (Sandbox Code Playgroud)\n\n看来您正在使用 PHP\xe2\x80\x94 编写 JavaScript,请小心。
\n