Last active
March 29, 2023 19:19
-
-
Save kooler62/e316cafd00810be0246eb5e4bfaf03e5 to your computer and use it in GitHub Desktop.
Human Count Number
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| if (!function_exists('humanCountNumber')) { | |
| /** | |
| * Generate the URL to a controller action. | |
| * @param int $count | |
| * @param string $delimiter | |
| * @return string | |
| * @example 3333 is '3.3 k' | 9950 is '10 k | |
| */ | |
| function humanCountNumber(int $count, string $delimiter = ' '): string | |
| { | |
| if ($count === 0) { | |
| return '0'; | |
| } | |
| $thousand = 1000; | |
| $million = 1_000_000; | |
| $billion = 1_000_000_000; | |
| return match (true) { | |
| $count >= $billion => round($count / $billion, 1) . "{$delimiter}B", | |
| $count >= $million => round($count / $million, 1) . "{$delimiter}M", | |
| $count >= $thousand => round($count / $thousand, 1) . "{$delimiter}k", | |
| default => "$count", | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment