Created
July 30, 2024 08:08
-
-
Save DerekBuntin/f78de4eb1ad90bc3c33dcc61bd02d5b5 to your computer and use it in GitHub Desktop.
Short Number Formatter for PHP & Laravel Helper (1000 to 1K; 1M; 1B; 1T)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Format numbers to their abbreviated form | |
* | |
* Converts the given number to an abbreviated string representation, adding a suffix of 'K', 'M', 'B', or 'T' | |
* to represent thousands, millions, billions, and trillions, respectively. | |
* | |
* @param float $number The number to format | |
* @param int $precision The decimal precision to use for the formatted number | |
* @return string The abbreviated string representation of the number | |
*/ | |
function numberFormatShort(float $number, int $precision = 1): string { | |
// The match expression determines the divisor and suffix to use based on the size of the number | |
[$divisor, $suffix] = match(true) { | |
// If the number is less than 900, no suffix or divisor is needed | |
$number < 900 => [1,''], | |
// If the number is less than 900,000, use 'K' as the suffix and divide by 1,000 | |
$number < 900000 => [1e3, 'K'], | |
// If the number is less than 900,000,000, use 'M' as the suffix and divide by 1,000,000 | |
$number < 900000000 => [1e6, 'M'], | |
// If the number is less than 900,000,000,000, use 'B' as the suffix and divide by 1,000,000,000 | |
$number < 900000000000 => [1e9, 'B'], | |
// If the number is 900,000,000,000 or more, use 'T' as the suffix and divide by 1,000,000,000,000 | |
default => [1e12, 'T'] | |
}; | |
// Divide the number by the determined divisor and format it to the specified precision | |
$formattedNumber = number_format($number / $divisor, $precision); | |
// If the precision is greater than 0, remove unnecessary trailing zeros after the decimal point | |
if ($precision > 0) { | |
$dotzero = '.' . str_repeat('0', $precision); // Get the string of the form ".00" | |
$formattedNumber = str_replace($dotzero, '', $formattedNumber); // Replace ".00" with "" in the formatted number | |
} | |
// Return the formatted number with the determined suffix | |
return $formattedNumber . $suffix; | |
} | |
/* Example Usage */ | |
echo numberFormatShort(1250); // Prints: '1.3K' | |
echo numberFormatShort(540000); // Prints: '540K' | |
echo numberFormatShort(5950000); // Prints: '6M' | |
echo numberFormatShort(670000000); // Prints: '670M' | |
echo numberFormatShort(7800000000); // Prints: '7.8B' | |
$totalUsers = 12456; | |
echo "The site has ". numberFormatShort($totalUsers) . " users"; | |
// Outputs: The site has 12.5K users | |
/* Laravel Helper: MoneyHelper::numberFormatShort */ | |
namespace App\Helpers; | |
class MoneyHelper { | |
static function numberFormatShort(float $number, int $precision = 1): string { | |
[$divisor, $suffix] = match(true) { | |
$number < 900 => [1,''], | |
$number < 900000 => [1e3, 'K'], | |
$number < 900000000 => [1e6, 'M'], | |
$number < 900000000000 => [1e9, 'B'], | |
default => [1e12, 'T'] | |
}; | |
$numberFormat = number_format($number / $divisor, $precision); | |
if ($precision > 0) { | |
$dotzero = '.' . str_repeat('0', $precision); | |
$numberFormat = str_replace($dotzero, '', $numberFormat); | |
} | |
return $numberFormat . $suffix; | |
} | |
} | |
/* Usage */ | |
use App\Helpers\MoneyHelper; | |
$totalUsers = 12456; | |
echo "The site has ". MoneyHelper::numberFormatShort($totalUsers) . " users"; | |
// Outputs: The site has 12.5K users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment