Sometimes your code produces large numbers that are hard for users to read. For example, 17753392783 bytes or 18293753038 units. For human eyes, it’s easier to read “17MB” or “1 million” rather than a string of digits.
I wrote this as a quick tip in case you want to convert your data to a human-readable format. I’m doing this in PHP; for other programming languages you can take the algorithm and convert it to your own language:
function convert($numbers)
{
$readable = array("", "thousands", "millions", "billions");
$index=0;
while($numbers > 1000){
$numbers /= 1000;
$index++;
}
return("".round($numbers, 2)." ".$readable [$index]);
}
Usage:
echo convert(199000000);
// Will show 199.00 millions
Thank you for reading this article. Good luck and have a nice day.
