I've modified Kyle Challis answer with the following changes/fixes:
- Trailing 0's are removed. 1.00K becomes 1K.
- If using the "0 + number_format(...)" solution within the comments, it would've caused a
A non-numeric value encountered error if calling numberShorten(999_999, 2). This has been fixed by using rtrim instead.
- It rounds down. I found it didn't really make too much sense for 1,099 (
numberShorten(1_099,1)) to result in 1.1K. If 1.1K is returned, I would expect the value to be 1.1K as minimum.
/**
* Shorten a given number.
*
* For example: 1,000,000 will be returned as 1M
*
* @param int|float $number
* @param int $precision
* @param array|null $divisors
* @return string
*/
function numberShorten($number, $precision = 3, $divisors = null): string
{
// Setup default $divisors if not provided
if (!isset($divisors)) {
$divisors = array(
pow(1000, 0) => '', // 1000^0 == 1
pow(1000, 1) => 'K', // Thousand
pow(1000, 2) => 'M', // Million
pow(1000, 3) => 'B', // Billion
pow(1000, 4) => 'T', // Trillion
pow(1000, 5) => 'Qa', // Quadrillion
pow(1000, 6) => 'Qi', // Quintillion
);
}
// Loop through each $divisor and find the
// lowest amount that matches
foreach ($divisors as $divisor => $shorthand) {
if (abs($number) < ($divisor * 1000)) {
// We found a match!
break;
}
}
// We found our match, or there were no matches.
// Either way, use the last defined value for $divisor.
// We floor the number so values like 1099 don't return as 1.1k
$final_number = floor($number / $divisor);
if ($precision > 0) {
$final_number += floor(($number % $divisor) / ($divisor / pow(10, $precision))) / pow(10, $precision);
}
$final_number = number_format($final_number, $precision);
// Using rtrim() to remove any trailing insignificant 0's
$final_number = rtrim(rtrim($final_number, '0'), '.');
return $final_number . $shorthand;
}
I also created a few tests if you want to see the output:
# Precision as "1"
assertEquals("1", numberShorten(1, 1));
assertEquals("999", numberShorten(999, 1));
assertEquals("1K", numberShorten(1_000, 1));
assertEquals("1K", numberShorten(1_010, 1));
assertEquals("1.1K", numberShorten(1_190, 1));
assertEquals("1M", numberShorten(1_000_000, 1));
assertEquals("1M", numberShorten(1_010_000, 1));
assertEquals("1.9M", numberShorten(1_999_999, 1));
# Precision as "2"
assertEquals("1", numberShorten(1, 2));
assertEquals("999", numberShorten(999, 2));
assertEquals("1K", numberShorten(1_000, 2));
assertEquals("1K", numberShorten(1_001, 2));
assertEquals("1K", numberShorten(1009, 2));
assertEquals("1.01K", numberShorten(1_010, 2));
assertEquals("900K", numberShorten(900_000, 2));
assertEquals("900K", numberShorten(900_009, 2));
assertEquals("900.01K", numberShorten(900_010, 2));
assertEquals("999K", numberShorten(999_000, 2));
assertEquals("999.99K", numberShorten(999_999, 2));
assertEquals("1M", numberShorten(1_000_000, 2));
assertEquals("1M", numberShorten(1_000_001, 2));
assertEquals("1M", numberShorten(1_009_999, 2));
assertEquals("1.99M", numberShorten(1_999_999, 2));
assertEquals("1.01M", numberShorten(1_010_000, 2));
assertEquals("1.01B", numberShorten(1_010_000_000, 2));
assertEquals("1.01T", numberShorten(1_010_000_000_000, 2));
Kilo/Mega/Giga, notThousand/Million/Billion?