Skip to content

Instantly share code, notes, and snippets.

@dcangulo
Last active April 3, 2018 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcangulo/4e6e6e38585dd1ac00843beb4e341301 to your computer and use it in GitHub Desktop.
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.
<?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