An alternative method is to have the abbreviations in an array and use power of to calculate the number to multiply with.
This gives a shorter code if you have lots of abbreviations.
I use strtoupper to make sure it matches both k
and 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
}
https://3v4l.org/LXVXN