【发布时间】:2020-02-15 11:00:37
【问题描述】:
您将如何创建一个函数(在 PHP 中)以返回适当的舍入排名组的值(例如“前 5”、“前 10”、“前 15”、...“前 100 万”、“前 500 万”、“前 1000 万”、“前 1500 万”)。
请注意,它应该四舍五入到最接近的 1、5 或 10 - 例如“前 1500 万”而不是“前 1200 万”。
例子:
排名 = 输出
- 1-5 = 5(前 5 名)
- 6-10 = 10(前 10 名)
- 11-15 = 15(排名前 15)
这应该一直有效到数万亿。
我当前的函数工作(有点)但是它会返回一个前 15 名的排名组作为前 20 名的值。
private function format_number_iollions($amount,$style=null) {
$amount = (0 + str_replace(',', '', $amount));
if (!is_numeric($amount)){
return false;
}
$plusString = '';
switch ($style){
case 'plus':
$plusString = '+';
break;
}
if ($style==='rank' && $amount<=10){
return 10;
}
// filter and format it
if ($amount>1000000000000){
if ($style==='rank'){
$v = ceil(($amount/1000000000000));
} else {
$v = floor(($amount/1000000000000));
}
$v .= $plusString.' trillion';
} else if ($amount>1000000000){
if ($style==='rank'){
$v = ceil(($amount/1000000000));
} else {
$v = floor(($amount/1000000000));
}
$v .= $plusString.' billion';
} else if ($amount>1000000){
if ($style==='rank'){
$v = ceil(($amount/1000000));
} else {
$v = floor(($amount/1000000));
}
$v .= $plusString.' million';
} else if ($amount>100000){
if ($style==='rank'){
$v = ceil(($amount/100000));
} else {
$v = floor(($amount/100000));
}
$v .= '00,000'.$plusString;
} else if ($amount>10000){
if ($style==='rank'){
$v = ceil(($amount/10000));
} else {
$v = floor(($amount/10000));
}
$v .= '0,000'.$plusString;
} else if ($amount>1000){
if ($style==='rank'){
$v = ceil(($amount/1000));
} else {
$v = floor(($amount/1000));
}
$v .= ',000'.$plusString;
} else if ($amount>100){
if ($style==='rank'){
$v = ceil(($amount/100));
} else {
$v = floor(($amount/100));
}
$v .= '00'.$plusString;
} else if ($amount>10){
if ($style==='rank'){
$v = ceil(($amount/10));
} else {
$v = floor(($amount/10));
}
$v .= '0'.$plusString;
} else {
return number_format($amount);
}
return $v;
}
UPDATE - 最终解决方案是这个功能(如果有人需要的话):
private function get_rank_group($rawrank) {
// Divide by 1000 and count how many divisions were done
$rank_scale = 0;
while ($rawrank >= 1000) {
$rawrank /= 1000;
$rank_scale++;
}
// Determine which Top X can be
if ($rawrank >= 100) {
$lim_name = (floor(($rawrank-1) / 50) + 1) * 50;
} else {
$lim_name = (floor(($rawrank-1) / 5) + 1) * 5;
}
// if its in the next higher level
if ($lim_name >= 1000) {
$lim_name = '1';
$rank_scale++;
}
static $rank_scale_names = array('', ',000', ' Million', ' Billion', ' Trillion', ' Quadrillion', ' Quintillion', ' Sextillion', ' Septillion');
if (!isset($rank_scale_names[$rank_scale])){
return null; //too much - add higher word-numbers to $rank_scale_names
} else {
return "$lim_name{$rank_scale_names[$rank_scale]}";
}
}
【问题讨论】:
-
使用 PHP 的
%操作符。 -
至少我不确定我完全理解应该是什么输出。您能否提供几个带有
$amount输入和所需输出的示例?