Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

size_format() #4

Open
KarelWintersky opened this issue Nov 2, 2024 · 0 comments
Open

size_format() #4

KarelWintersky opened this issue Nov 2, 2024 · 0 comments

Comments

@KarelWintersky
Copy link
Contributor

/**
 *
 * @param int $size
 * @param int $decimals
 * @param string $decimal_separator
 * @param string $thousands_separator
 * @return string
 */
function size_format(int $size, int $decimals = 0, string $decimal_separator = '.', string $thousands_separator = ','): string {
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    $index = min(floor((strlen(strval($size)) - 1) / 3), count($units) - 1);
    $number = number_format($size / pow(1000, $index), $decimals, $decimal_separator, $thousands_separator);
    return sprintf('%s %s', $number, $units[$index]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant