Skip to content

Instantly share code, notes, and snippets.

@azharisubroto
Created September 8, 2016 09:23
Show Gist options
  • Save azharisubroto/e47b0af6857e4833c3b20d36aedd35f0 to your computer and use it in GitHub Desktop.
Save azharisubroto/e47b0af6857e4833c3b20d36aedd35f0 to your computer and use it in GitHub Desktop.
fancy number
<?php
function zl_fancy_number($n, $precision = 1) {
if ($n < 1000) {
// Anything less than a thousand
$n_format = number_format($n);
} elseif ($n > 1000 && $n < 10000) {
$n_format = number_format($n / 1000, $precision) . 'K';
} elseif ($n < 1000000000) {
// Anything less than a billion
$n_format = number_format($n / 1000000, $precision) . 'M';
} else {
// At least a billion
$n_format = number_format($n / 1000000000, $precision) . 'B';
}
return $n_format;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment