Currently the channel with most subscribers hasĀ 65119648 subscribers. Probably you didn't read the number easily, so an abbreviation like 65M or 6511K would be easier to read isn't? A lot of people really estimate the utility of such feature, so if your website or application handles long numbers that nobody would like to read completely, just show them an abreviature.
In this article, we'll show you 2 implementations to generate the abbreviation of a number with PHP.
A. Exact abbreviation
If you are willing to display exactly the abbreviation of the providen number, this implementation will do the trick:
<?php
/**
* Function that converts a numeric value into an exact abbreviation
*/
function number_format_short( $n, $precision = 1 ) {
if ($n < 900) {
// 0 - 900
$n_format = number_format($n, $precision);
$suffix = '';
} else if ($n < 900000) {
// 0.9k-850k
$n_format = number_format($n / 1000, $precision);
$suffix = 'K';
} else if ($n < 900000000) {
// 0.9m-850m
$n_format = number_format($n / 1000000, $precision);
$suffix = 'M';
} else if ($n < 900000000000) {
// 0.9b-850b
$n_format = number_format($n / 1000000000, $precision);
$suffix = 'B';
} else {
// 0.9t+
$n_format = number_format($n / 1000000000000, $precision);
$suffix = 'T';
}
// Remove unecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1"
// Intentionally does not affect partials, eg "1.50" -> "1.50"
if ( $precision > 0 ) {
$dotzero = '.' . str_repeat( '0', $precision );
$n_format = str_replace( $dotzero, '', $n_format );
}
return $n_format . $suffix;
}
This means, displaying the abbreviation with decimals e.g
<?php
$examples = array(15, 129, 400, 1500, 14350, 30489, 50222, 103977 , 2540388, 53003839);
foreach($examples as $example){
echo "$example => " . number_format_short($example) . "\n";
}
/*
Outputs:
15 => 15
129 => 129
400 => 400
1500 => 1.5K
14350 => 14.4K
30489 => 30.5K
50222 => 50.2K
103977 => 104K
2540388 => 2.5M
53003839 => 53M
*/
This implementation uses theĀ number_format
function, which formats a number with grouped thousands.
B. General abbreviation
If you are willing to display only the important part of the providen number (without exact group of thousand), this implementation will do the trick:
<?php
/**
* Function that converts a numeric value into an abbreviation.
*/
function number_format_short( $n ) {
if ($n > 0 && $n < 1000) {
// 1 - 999
$n_format = floor($n);
$suffix = '';
} else if ($n >= 1000 && $n < 1000000) {
// 1k-999k
$n_format = floor($n / 1000);
$suffix = 'K+';
} else if ($n >= 1000000 && $n < 1000000000) {
// 1m-999m
$n_format = floor($n / 1000000);
$suffix = 'M+';
} else if ($n >= 1000000000 && $n < 1000000000000) {
// 1b-999b
$n_format = floor($n / 1000000000);
$suffix = 'B+';
} else if ($n >= 1000000000000) {
// 1t+
$n_format = floor($n / 1000000000000);
$suffix = 'T+';
}
return !empty($n_format . $suffix) ? $n_format . $suffix : 0;
}
Instead of generating an abbreviation with "decimals", the snippet will add the main number and a plus symbol e.g:
<?php
$examples = array(15, 129, 400, 1500, 14350, 30489, 50222, 103977 , 2540388, 53003839);
foreach($examples as $example){
echo "$example => " . number_format_short($example) . "\n";
}
/*
Outputs:
15 => 15
129 => 129
400 => 400
1500 => 1K+
14350 => 14K+
30489 => 30K+
50222 => 50K+
103977 => 103K+
2540388 => 2M+
53003839 => 53M+
*/
Happy coding !