Php 将K格式的千位转换为常规千位格式

Php 将K格式的千位转换为常规千位格式,php,number-formatting,Php,Number Formatting,我有以下格式的数字: 12.2K 我想将此数字转换为显示: 12200 转换为K格式,但我想从K格式转换 有没有一个简单的方法可以做到这一点 谢谢 你是说,像这样的事情?这将能够转化成成千上万的人,等等 <?php $s = "12.2K"; if (strpos(strtoupper($s), "K") != false) { $s = rtrim($s, "kK"); echo floatval($s) * 1000; } else if (strpos

我有以下格式的数字:

12.2K
我想将此数字转换为显示:

12200
转换为K格式,但我想从K格式转换

有没有一个简单的方法可以做到这一点


谢谢

你是说,像这样的事情?这将能够转化成成千上万的人,等等

<?php
  $s = "12.2K";
  if (strpos(strtoupper($s), "K") != false) {
    $s = rtrim($s, "kK");
    echo floatval($s) * 1000;
  } else if (strpos(strtoupper($s), "M") != false) {
    $s = rtrim($s, "mM");
    echo floatval($s) * 1000000;
  } else {
    echo floatval($s);
  }
?>

您也可以通过其他字母等展开此项。


<?php
$number = '12.2K';

if (strpos($number, 'K') !== false)
    {
    $number = rtrim($number, 'K') * 1000;
    }

echo $number
?>

基本上,您只需要检查字符串是否包含某个字符,如果包含,则通过将其取出并乘以1000来响应它。

另一种方法是将缩写词放入数组中,并使用的幂来计算要乘以的数字。
如果有很多缩写,则代码会更短。
我使用strtoupper来确保它同时匹配
k
k

$arr = ["K" => 1 ,"M" => 2, "T" => 3]; // and so on for how ever long you need

$input = "12.2K";
if(isset($arr[strtoupper(substr($input, -1))])){ //does the last character exist in array as an key
    echo substr($input,0,-1) * pow(1000, $arr[strtoupper(substr($input, -1))]); //multiply with the power of the value in array
    //      12.2             *       1000^1
}else{
    echo $input; // less than 1k, just output
}

这不是代码编写服务。一旦你有了一些代码,问一个更具体的问题。当输入是12k,那么?
$arr = ["K" => 1 ,"M" => 2, "T" => 3]; // and so on for how ever long you need

$input = "12.2K";
if(isset($arr[strtoupper(substr($input, -1))])){ //does the last character exist in array as an key
    echo substr($input,0,-1) * pow(1000, $arr[strtoupper(substr($input, -1))]); //multiply with the power of the value in array
    //      12.2             *       1000^1
}else{
    echo $input; // less than 1k, just output
}