Skip to content

Instantly share code, notes, and snippets.

@gerharddt
Created September 7, 2020 08:58
Show Gist options
  • Save gerharddt/8ade1bbc76c2521e3e35b87dc5ca7e80 to your computer and use it in GitHub Desktop.
Save gerharddt/8ade1bbc76c2521e3e35b87dc5ca7e80 to your computer and use it in GitHub Desktop.
PHP: Nice human readable big numbers
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