summaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authorSimon Rettberg2018-03-21 11:48:18 +0100
committerSimon Rettberg2018-03-21 11:48:18 +0100
commit7780372ee5c8f819c0ad6c6c637254496de42d0b (patch)
tree85d1d4dd4295773c218fe6ce63141a7ae76dede2 /inc
parent[translation] Fix tag collision for messages (diff)
downloadslx-admin-7780372ee5c8f819c0ad6c6c637254496de42d0b.tar.gz
slx-admin-7780372ee5c8f819c0ad6c6c637254496de42d0b.tar.xz
slx-admin-7780372ee5c8f819c0ad6c6c637254496de42d0b.zip
[inc/Util] readableFileSize: Use thin space, determine decimals after division
Diffstat (limited to 'inc')
-rw-r--r--inc/util.inc.php14
1 files changed, 9 insertions, 5 deletions
diff --git a/inc/util.inc.php b/inc/util.inc.php
index c0617310..52e1dbca 100644
--- a/inc/util.inc.php
+++ b/inc/util.inc.php
@@ -234,18 +234,22 @@ SADFACE;
*
* @param float|int $bytes numeric value of the filesize to make readable
* @param int $decimals number of decimals to show, -1 for automatic
- * @return string human readable string representing the given filesize
+ * @return string human readable string representing the given file size
*/
public static function readableFileSize($bytes, $decimals = -1)
{
+ $bytes = round($bytes);
static $sz = array('Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$factor = (int)floor((strlen($bytes) - 1) / 3);
- if ($factor == 0) {
+ if ($factor === 0) {
$decimals = 0;
- } elseif ($decimals === -1) {
- $decimals = 2 - floor((strlen($bytes) - 1) % 3);
+ } else {
+ $bytes = round($bytes / pow(1024, $factor));
+ if ($decimals === -1) {
+ $decimals = 2 - floor(strlen((int)$bytes) - 1);
+ }
}
- return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . $sz[$factor];
+ return sprintf("%.{$decimals}f", $bytes) . "\xe2\x80\x89" . $sz[$factor];
}
public static function sanitizeFilename($name)