Last active
April 3, 2018 06:15
-
-
Save dcangulo/4e6e6e38585dd1ac00843beb4e341301 to your computer and use it in GitHub Desktop.
A PHP function that abbreviates numbers. (Eg. 1000 to 1K+). Visit https://www.davidangulo.xyz/ for more information.
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 | |
function abbreviateNumber($num) { | |
if ($num >= 0 && $num < 1000) { | |
$format = floor($num); | |
$suffix = ''; | |
} | |
else if ($num >= 1000 && $num < 1000000) { | |
$format = floor($num / 1000); | |
$suffix = 'K+'; | |
} | |
else if ($num >= 1000000 && $num < 1000000000) { | |
$format = floor($num / 1000000); | |
$suffix = 'M+'; | |
} | |
else if ($num >= 1000000000 && $num < 1000000000000) { | |
$format = floor($num / 1000000000); | |
$suffix = 'B+'; | |
} | |
else if ($num >= 1000000000000) { | |
$format = floor($num / 1000000000000); | |
$suffix = 'T+'; | |
} | |
return !empty($format . $suffix) ? $format . $suffix : 0; | |
} | |
//Visit the tutorial for more information | |
//https://www.davidangulo.xyz/website-development/how-to-abbreviate-numbers-in-php/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment