numberformater php format to k and m 1

numberformater php format to k and m

function shortNumber($num) 
{
    $units = ['', 'K', 'M', 'B', 'T'];
    for ($i = 0; $num >= 1000; $i++) {
        $num /= 1000;
    }
    return round($num, 1) . $units[$i];
}

Here is what the above code is Doing:
1. We’re using a for loop to iterate through the $units array.
2. We’re dividing the $num by 1000 each time we iterate through the loop.
3. We’re returning the $num and the $units[$i] value.

Similar Posts