numberformater php format to k and m code example
Example 1: numberformater php format to k and m
function shortNumber($num)
{
$units = ['', 'K', 'M', 'B', 'T'];
for ($i = 0; $num >= 1000; $i++) {
$num /= 1000;
}
return round($num, 1) . $units[$i];
}
Example 2: numberformater php format to k and m
function number_format_short( $n ) {
if ($n > 0 && $n < 1000) {
$n_format = floor($n);
$suffix = '';
} else if ($n >= 1000 && $n < 1000000) {
$n_format = floor($n / 1000);
$suffix = 'K+';
} else if ($n >= 1000000 && $n < 1000000000) {
$n_format = floor($n / 1000000);
$suffix = 'M+';
} else if ($n >= 1000000000 && $n < 1000000000000) {
$n_format = floor($n / 1000000000);
$suffix = 'B+';
} else if ($n >= 1000000000000) {
$n_format = floor($n / 1000000000000);
$suffix = 'T+';
}
return !empty($n_format . $suffix) ? $n_format . $suffix : 0;
}
Example 3: numberformater php format to k and m
<?php
$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
$fmt = new NumberFormatter( 'ru_RU', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
?>