how to convert number into sort form in php

How to Convert 1000 to 1k |using PHP

Welcome back to shorltearner.com, in our previous post we learn how to Convert words to numbers with the help of PHP. so in this post today we will learn how to convert number into sort number format in PHP.
so before start this tutorial we take an overview on number_format which is also a predefined PHP function that are used to The number_format() function formats a number with grouped thousands.

how to convert number into sort form in php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

for example if i take the below code as an example

<?php echo number_format("1000000"); ?>

than the number_format function returns an output like

outout : 1,000,000

so If we want to convert number into sort form in thousand, million, billion then we used another predefined PHP function number_format_short that will help to convert any number to it’s sort form like (Eg: 1.4K, 14.32M, 40B ) etc.
so in the below code we take an example which converts a number 1,43,20000 into 14.32M.

<?php 
    function number_format_short($n) {
        // first strip any formatting;
        $n = (0+str_replace(",", "", $n));
        // is this a number?
        if (!is_numeric($n)) return false;
         // now filter it;
        if ($n > 1000000000000) return round(($n/1000000000000), 2).'T';
        elseif ($n > 1000000000) return round(($n/1000000000), 2).'B';
        elseif ($n > 1000000) return round(($n/1000000), 2).'M';
        elseif ($n > 1000) return round(($n/1000), 2).'K';
 
        return number_format($n);
    }
echo number_format_short('14320000'); //14.32M

?>