Skip to content

Instantly share code, notes, and snippets.

@cryocaustik
Created December 27, 2017 19:44
Show Gist options
  • Save cryocaustik/8e5737c719083993872b50f8d6395c9d to your computer and use it in GitHub Desktop.
Save cryocaustik/8e5737c719083993872b50f8d6395c9d to your computer and use it in GitHub Desktop.
convert large numbers to abbreviations
<?php
function numFormat($number) {
if(!$number){
return $number;
}
$abbrevs = array(18 => "Q", 15 => "q", 12 => "t", 9 => "B", 6 => "M", 3 => "K", 0 => "");
foreach($abbrevs as $exponent => $abbrev) {
if($number >= pow(10, $exponent)) {
$display_num = $number / pow(10, $exponent);
$decimals = ($exponent >= 3 && round($display_num) < 100) ? 1 : 0;
return number_format($display_num,$decimals) . $abbrev;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment