Skip to content

Instantly share code, notes, and snippets.

@luckyshot
Created September 30, 2013 01:20
Show Gist options
  • Save luckyshot/6758239 to your computer and use it in GitHub Desktop.
Save luckyshot/6758239 to your computer and use it in GitHub Desktop.
Nice number abbreviation (i.e. 12.5k)
function abbreviate_number($num) {
if ($num >= pow(10,3) AND $num < pow(10,4)) {
return round($num/1000, 1).'k';
}else if ($num >= pow(10,4) AND $num < pow(10,5)) {
return round($num/1000, 1).'k';
}else if ($num >= pow(10,5) AND $num < pow(10,6)) {
return round($num/1000, 0).'k';
}else if ($num >= pow(10,6) AND $num < pow(10,7)) {
return round($num/1000000, 1).'m';
}else if ($num >= pow(10,7) AND $num < pow(10,8)) {
return round($num/1000000, 0).'m';
}else if ($num >= pow(10,8)) {
return round($num/1000000, 0).'m';
}else{
return $num;
}
}
echo abbreviate_number(123456789);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment