Created
September 7, 2020 08:58
-
-
Save gerharddt/8ade1bbc76c2521e3e35b87dc5ca7e80 to your computer and use it in GitHub Desktop.
PHP: Nice human readable big numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bd_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),1).' trillion'; | |
else if($n>1000000000) return round(($n/1000000000),1).' billion'; | |
else if($n>1000000) return round(($n/1000000),1).' million'; | |
else if($n>1000) return round(($n/1000),1).' thousand'; | |
return number_format($n); | |
} | |
// Outputs: | |
// 247,704,360 -> 247.7 million | |
// 866,965,260,000 -> 867 billion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment