Skip to content

Instantly share code, notes, and snippets.

@kooler62
Last active March 29, 2023 19:19
Show Gist options
  • Select an option

  • Save kooler62/e316cafd00810be0246eb5e4bfaf03e5 to your computer and use it in GitHub Desktop.

Select an option

Save kooler62/e316cafd00810be0246eb5e4bfaf03e5 to your computer and use it in GitHub Desktop.
Human Count Number
<?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