Manage numbers
/**
* Format a number with grouped thousands like 8B, 23M, 50K.
* @param float $number The number being formatted.
* @param integer $decimals Sets the number of decimal points.
* @param string $dec_point Sets the separator for the decimal point.
* @param string $thousands_sep Sets the thousands separator.
* @return string A formatted version of number.
*/
function custom_number_format($number, $decimals = 0, $dec_point = '.', $thousands_sep = ' ') {
if (is_numeric($number)) {
if ($number < 1000) {
// Anything less than a thousand
$formatted = number_format($number);
}
else if ($number < 1000000) {
// Anything less than a million
$formatted = number_format($number / 1000, $decimals, $dec_point, $thousands_sep) . 'K';
}
else if ($number < 1000000000) {
// Anything less than a billion
$formatted = number_format($number / 1000000, $decimals, $dec_point, $thousands_sep) . 'M';
}
else {
// At least a billion
$formatted = number_format($number / 1000000000, $decimals, $dec_point, $thousands_sep) . 'B';
}
return $formatted;
}
return $number;
}