- is_numeric: documentation ( source)
- function_exists: documentation ( source)
- round: documentation ( source)
- str_replace: documentation ( source)
- number_format: documentation ( source)
<?php
if (!function_exists('nice_number')) {
function nice_number($n) {
// first strip any formatting;
$n = (0 + str_replace(",", "", $n));
// is this a number?
if (!is_numeric($n))
return false;
// now filter it;
$str = '';
if ($n > 1000000000000) {
$str = round(($n / 1000000000000), 0) . ' tn ';
$str .= $n % 1000000000000 > 0 ? nice_number($n % 1000000000000) : '';
return $str;
}
else if ($n > 1000000000) {
$str = round(($n / 1000000000), 0) . ' bn ';
$str .= $n % 1000000000 > 0 ? nice_number($n % 1000000000) : '';
return $str;
}
else if ($n > 1000000) {
$str = round(($n / 1000000), 0) . ' mn ';
$str .= $n % 1000000 > 0 ? nice_number($n % 1000000) : '';
return $str;
}
else if ($n > 1000)
return round(($n / 1000), 0) . ' K ';
return number_format($n);
}
}
echo nice_number(300000000);