Faster PHP Cloud Hosting

Last night I was writing some php code in which I had to multiply numbers with hundreds or thousands which resulted in very huge numbers and it was really difficult for me to read it. Therefore I created a function named nice_numbers to which converts any number into human readable form.

Here is my function :-

function nice_number($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).' Tn';
 elseif ($n > 1000000000) return round(($n/1000000000), 2).' Bn';
 elseif ($n > 1000000) return round(($n/1000000), 2).' M';
 elseif ($n > 1000) return round(($n/1000), 2).' K';

return number_format($n);

NOTE : You can copy and use this function for anywhere you want to.

You can pass any number to the nice_number function and get it converted into human readable form.

Example Usage :-

echo nice_number(10000000); // will print the number in human readable form

How Does The Function Work

The function isn’t really tough it just like rounds up your number with a base of 1000, 1000000 , etc.

Fore example –

$num = 1000 ;

$round = round($num,1000) ;

$human_readable = $round.' K' ;

echo $human_readable ; // 1 K


And that’s it I hope this was all that you needed. In case if you didn’t get something ask me in the comments section below.

Here are some other awesome posts for you –