-
-
Save warmwhisky/3b4d506cf4e5a440acb5d6ac9ba7fc9d to your computer and use it in GitHub Desktop.
Shorten large numbers into abbreviations (i.e. 1,500 = 1.5k)
This file contains 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
<?php | |
/** | |
* Shorten large numbers into abbreviations (i.e. 1,500 = 1.5k) | |
* | |
* @param int $number Number to shorten | |
* @return String A number with a symbol | |
*/ | |
function numberAbbreviation($number) { | |
$abbrevs = array(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