如果大于值,则以不同的方式回显整数


Echo Out Interger Differently If More Than Value PHP

所以我为我的网站创建了一个积分系统,我想在用户配置文件上显示时更改为回声而不是实际整数。例如:当整数小于1000时,它显示为实际数字(例如:645)。但是当它在1000和1100之间时,它会显示为"1k",以此类推。到目前为止,我得到的工作,但显示不正确,似乎有点浪费空间。有没有一种更简单的方法;更快的方法吗?

谢谢!

代码:

<?php
   $points_disp = $user_data['points'];
   if($points_disp < 1000){
      echo $points_disp;
   } else if ($points_disp >= 1000){
      echo '1k';
   } else if ($points_disp >= 1200){
      echo '1.2k';
   } else if ($points_disp >= 1400){
      echo '1.4k';
   } else if ($points_disp >= 1600){
      echo '1.6k';
   } else if ($points_disp >= 1800){
      echo '1.8k';
   } else if ($points_disp >= 2000){
      echo '2k';
   }
?>
Edit: I figured out an easier way to do this;
code (for anyone else who needs to do this):
<?php
$points_disp = $user_data['points'];
$fdigit = substr($points_disp, 0, 1);
$sdigit = substr($points_disp, 1, 1);
if ($points_disp < 1000){
    echo $points_disp;
} else if ($points_disp >= 1000){
    echo $fdigit . "." . $sdigit . "k";
}
echo $num; 
?>

可以使用切换大小写:

$points_disp = $user_data['points'];
switch(true)
{
    case ($points_disp < 1000):
     $num = $points_disp;
     break;
    case ($points_disp > 1000 && $points_disp < 1100 ):
        $num = '1.2k';
        break;
    //...so on
}
echo $num;

试试这个,

if($points_disp < 1000){
     echo $points_disp;
} else if($points_disp >= 1000) {
     echo round($points_disp/1000,1) . "K";
}