“6k 视图”是什么意思以及如何在 PHP 中格式化数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2703879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What Does '6k views' mean and how can I format the number in PHP
提问by Mostafa Elkady
What does "6k views" mean and how can I format this number in PHP?
“6k 视图”是什么意思,我如何在 PHP 中格式化这个数字?
回答by Gumbo
kis the abbreviation for the Kilo prefixand means thousand. So 6k means six thousand.
k是Kilo 前缀的缩写,意思是千。所以6k意味着六千。
You can format a number in such a way with the following function using division:
您可以使用以下函数使用除法以这种方式格式化数字:
function format($number) {
$prefixes = 'kMGTPEZY';
if ($number >= 1000) {
for ($i=-1; $number>=1000; ++$i) {
$number /= 1000;
}
return floor($number).$prefixes[$i];
}
return $number;
}
Or using logarithm base 10 and exponentiation:
或者使用以 10 为底的对数和幂运算:
function format($number) {
$prefixes = 'kMGTPEZY';
if ($number >= 1000) {
$log1000 = floor(log10($number)/3);
return floor($number/pow(1000, $log1000)).$prefixes[$log1000-1];
}
return $number;
}
回答by Geoff
'6k views' on StackOverflow refers to the number of views a question has received. It means 6000 views.
StackOverflow 上的“6k 次观看”是指一个问题收到的观看次数。这意味着 6000 次观看。
If you're looking to format a similar style number in php then try something like
如果您想在 php 中格式化类似的样式编号,请尝试类似
$number = "";
if( $value > 1000 )
{
$number .= floor($value / 1000) . "k";
} else {
$number .= $value;
}
echo $number . " views".
Obviously you can add cases for m, g and t views if desired.
显然,如果需要,您可以为 m、g 和 t 视图添加案例。
回答by Gordon
Abridged from http://tamlyn.org/2008/12/formatting-bytes-with-significant-figures-in-php/
节选自http://tamlyn.org/2008/12/formatting-bytes-with-Effective-figures-in- php/
/** Calculate $value to $sigFigs significant figures */
function sigFig($value, $sigFigs = 3) {
//convert to scientific notation e.g. 12345 -> 1.2345x10^4
//where $significand is 1.2345 and $exponent is 4
$exponent = floor(log10(abs($value))+1);
$significand = round(($value
/ pow(10, $exponent))
* pow(10, $sigFigs))
/ pow(10, $sigFigs);
return $significand * pow(10, $exponent);
}
/** Format $value with the appropriate SI prefix symbol */
function format($value, $sigFigs = 3)
{
//SI prefix symbols
$units = array('', 'k', 'M', 'G', 'T', 'P', 'E');
//how many powers of 1000 in the value?
$index = floor(log10($value)/3);
$value = $index ? $value/pow(1000, $index) : $value;
return sigFig($value, $sigFigs) . $units[$index];
}
Doing *11because *10is too obvious
这样做*11,因为*10太明显
for($number = 100; $number < 100000000000000000000; $number*=11) {
echo format($number), PHP_EOL;
}
gives
给
100 1.1k 12.1k 133k 1.46M 16.1M 177M 1.95G 21.4G 236G 2.59T 28.5T 314T 3.45P 38P 418P 4.59E 50.5E
If you need the decimals, use the above, else Gumbo's solution is more compact. Gives:
如果您需要小数,请使用上面的,否则 Gumbo 的解决方案更紧凑。给出:
100 1k 12k 133k 1M 16M 177M 1G 21G 235G 2T 28T 313T 3P 37P 417P 4E 50E
回答by cosy
$number="6000";
$val=($number/1000)."k";
//= 6k
or if the $number="6k";
或者如果 $number="6k";
echo str_replace("k","000",$number);
回答by Sarfraz
In 6k, the kmeans kilo(i hope you know) which equals to 6000. You replace the thousand figure with k, that's it. Hope that helps :)
在 中6k,等于的k手段kilo(我希望你知道)6000。你用 替换千位数字k,就是这样。希望有帮助:)
回答by Femaref
- k means 1000, so '6k views' = 6000 views.
- normally, on every access to a page, a counter in a database is increased by 1. This just gets queried on every page access and printed.
- after you queried the value, divide it by 1000 and add a 'k' to the number (if the number is > 1000).
- k 表示 1000,所以“6k 次观看”= 6000 次观看。
- 通常,每次访问页面时,数据库中的计数器都会增加 1。这只会在每次访问页面时查询并打印。
- 查询完值后,将其除以 1000 并在数字上添加“k”(如果数字 > 1000)。
回答by Alejandro Arias
function sigFig($value, $sigFigs = 3) {
setlocale(LC_ALL, 'it_IT@euro', 'it_IT', 'it');
$exponent = floor(log10(abs($value))+1);
$significand = round(($value
/ pow(10, $exponent))
* pow(10, $sigFigs))
/ pow(10, $sigFigs);
return $significand * pow(10, $exponent);
}
function format($value, $sigFigs = 3)
{
$numero = $value;
if ($numero > 9999) {
$units = array('', 'k', 'M', 'G', 'T', 'P', 'E');
$index = floor(log10($value)/3);
$value = $index ? $value/pow(1000, $index) : $value;
return sigFig($value, $sigFigs) . $units[$index];
}else{
return number_format($numero, 0, '', '.'); ;
}
//Resultados:
//9999 -> 9.999 views
//10000 -> 10k views
//10200 -> 10,2k views
}

