From 74f1db5c2670f7247abc8ea8bbb9b171c9e88f6a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 15 Nov 2022 17:20:20 +0100 Subject: [inc/Dictionary] Add locale-aware number formatting method --- inc/dictionary.inc.php | 25 ++++++++++++++++++++++ inc/util.inc.php | 6 +++--- modules-available/statistics/pages/machine.inc.php | 6 +++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/inc/dictionary.inc.php b/inc/dictionary.inc.php index e366207f..4c98c63b 100644 --- a/inc/dictionary.inc.php +++ b/inc/dictionary.inc.php @@ -63,6 +63,31 @@ class Dictionary define('LANG', $language); } + /** + * Format given number using country-specific decimal point and thousands + * separator. + * @param float $num Number to format + * @param int $decimals How many decimals to display + */ + public static function number(float $num, int $decimals = 0): string + { + static $dec = null, $tho = null; + if ($dec === null) { + if (LANG === 'de') { + $dec = ','; + $tho = '.'; + } elseif (LANG !== 'en' && file_exists("lang/" . LANG . "/format.txt")) { + $tmp = file_get_contents("lang/" . LANG . "/format.txt"); + $dec = $tmp[0]; + $tho = $tmp[1]; + } else { + $dec = '.'; + $tho = ','; + } + } + return number_format($num, $decimals, $dec, $tho); + } + /** * Get complete key=>value list for given module, file, language * diff --git a/inc/util.inc.php b/inc/util.inc.php index 06f60007..8bd48dd3 100644 --- a/inc/util.inc.php +++ b/inc/util.inc.php @@ -87,13 +87,13 @@ class Util } /** - * Convert given number to human readable file size string. + * Convert given number to human-readable file size string. * Will append Bytes, KiB, etc. depending on magnitude of number. * * @param float|int $bytes numeric value of the filesize to make readable * @param int $decimals number of decimals to show, -1 for automatic * @param int $shift how many units to skip, i.e. if you pass in KiB or MiB - * @return string human readable string representing the given file size + * @return string human-readable string representing the given file size */ public static function readableFileSize(float $bytes, int $decimals = -1, int $shift = 0): string { @@ -113,7 +113,7 @@ class Util $decimals = 2 - floor(strlen((int)$bytes) - 1); } } - return sprintf("%.{$decimals}f", $bytes) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); + return Dictionary::number($bytes, $decimals) . "\xe2\x80\x89" . ($sz[$factor + $shift] ?? '#>PiB#'); } public static function sanitizeFilename(string $name) diff --git a/modules-available/statistics/pages/machine.inc.php b/modules-available/statistics/pages/machine.inc.php index e72b96c8..f9724b5c 100644 --- a/modules-available/statistics/pages/machine.inc.php +++ b/modules-available/statistics/pages/machine.inc.php @@ -201,9 +201,9 @@ class SubPage $client['lastboot_s'] .= ' (Up ' . floor($uptime / 86400) . 'd ' . gmdate('H:i', $uptime) . ')'; } } - $client['gbram'] = round(ceil($client['mbram'] / 512) / 2, 1); - $client['gbtmp'] = round($client['id44mb'] / 1024); - $client['gbid45'] = round($client['id45mb'] / 1024); + $client['gbram'] = Dictionary::number(ceil($client['mbram'] / 512) / 2, 1); + $client['gbtmp'] = Dictionary::number($client['id44mb'] / 1024); + $client['gbid45'] = Dictionary::number($client['id45mb'] / 1024); foreach (['tmp', 'id45', 'swap', 'mem'] as $item) { if ($client['live_' . $item . 'size'] == 0) continue; -- cgit v1.2.3-55-g7522