Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,7 @@ protected function getFileList(string $path, string $relative): array
$size = filesize($path . '/' . $entry);

if ($size > 2048 || $size == 1024) {
$size = Lang::getTxt('size_kilobytes', [Lang::numberFormat($size / 1024)], file: 'General');
$size = Lang::getTxt('size_kilobytes', [Lang::numberFormat($size / 1024, 2)], file: 'General');
} else {
$size = Lang::getTxt('size_bytes', [Lang::numberFormat($size)], file: 'General');
}
Expand Down
16 changes: 10 additions & 6 deletions Sources/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -930,13 +930,10 @@ public static function sentenceList(array $list, string $type = 'and'): string
public static function numberFormat(int|float|string $number, ?int $decimals = null): string
{
if (!is_numeric($number)) {
throw new \ValueError();
}

if (\is_string($number)) {
$number = $number + 0;
return $number;
}

// Get the correct separator characters for the current language.
self::$decimal_separator = self::$txt['decimal_separator'] ?? null;
self::$digit_group_separator = self::$txt['digit_group_separator'] ?? null;

Expand All @@ -951,7 +948,14 @@ public static function numberFormat(int|float|string $number, ?int $decimals = n
}
}

$skeleton = \is_int($number) ? 'integer' : ':: .' . str_repeat('0', $decimals ?? 2);
$skeleton = match (true) {
// Told to use a specific number of decimal places.
!empty($decimals) => ':: .' . str_repeat('0', $decimals),
// Integers are easy.
ctype_digit((string) $number) => 'integer',
// Floats default to as much precision as necessary.
default => ':: .0*',
};

return MessageFormatter::formatMessage('{0, number, ' . $skeleton . '}', [$number]);
}
Expand Down
44 changes: 29 additions & 15 deletions Sources/Localization/MessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ protected static function applyNumberSkeleton(int|float|string $number, string $
usort($tokens, fn($a, $b) => array_search(str_starts_with($a[0], '.') || str_starts_with($a[0], '@') ? substr($a[0], 0, 1) : $a[0], $preferred_order) <=> array_search(str_starts_with($b[0], '.') || str_starts_with($b[0], '@') ? substr($b[0], 0, 1) : $b[0], $preferred_order));

// A few variables that will affect how we manipulate and format numbers below.
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, PHP_ROUND_HALF_EVEN);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, PHP_ROUND_HALF_EVEN);
$group = 'thousands';
$flags = '0';

Expand All @@ -673,8 +673,15 @@ protected static function applyNumberSkeleton(int|float|string $number, string $

// Float precision format.
if (str_starts_with($stem, '.')) {
$significant_integers = \strlen(\strval(\intval($number + 0)));
$significant_decimals = (int) strpos(strrev(\strval($number)), '.');
// Special handling if $number is in scientific notation.
if (stripos(\strval($number), 'E') !== false) {
list($base, $exponent) = explode('E', strtoupper(\strval($number)));
$significant_integers = max(1, (int) $exponent);
$significant_decimals = (\strlen($base) - 2) + max(-$exponent, 0);
} else {
$significant_integers = \strlen(\strval(\intval($number + 0)));
$significant_decimals = (int) strpos(strrev(\strval($number)), '.');
}

preg_match('/\.(0*)(#*)(\*?)/', $stem, $matches);

Expand Down Expand Up @@ -724,8 +731,15 @@ protected static function applyNumberSkeleton(int|float|string $number, string $
}
// Significant digits format.
elseif (str_starts_with($stem, '@')) {
$significant_integers = \strlen(\strval(\intval($number + 0)));
$significant_decimals = (int) strpos(strrev(\strval($number)), '.');
// Special handling if $number is in scientific notation.
if (stripos(\strval($number), 'E') !== false) {
list($base, $exponent) = explode('E', strtoupper(\strval($number)));
$significant_integers = max(1, (int) $exponent);
$significant_decimals = (\strlen($base) - 2) + max(-$exponent, 0);
} else {
$significant_integers = \strlen(\strval(\intval($number + 0)));
$significant_decimals = (int) strpos(strrev(\strval($number)), '.');
}

preg_match('/(@+)(#*)(\*?)/', $stem, $matches);

Expand All @@ -752,44 +766,44 @@ protected static function applyNumberSkeleton(int|float|string $number, string $
} else {
switch ($stem) {
case 'rounding-mode-ceiling':
$round = fn(int|float $number, int $precision = 0) => ceil($number * (10 ** $precision)) / (10 ** $precision);
$round = fn(int|float|string $number, int $precision = 0) => ceil($number * (10 ** $precision)) / (10 ** $precision);
break;

case 'rounding-mode-floor':
$round = fn(int|float $number, int $precision = 0) => floor($number * (10 ** $precision)) / (10 ** $precision);
$round = fn(int|float|string $number, int $precision = 0) => floor($number * (10 ** $precision)) / (10 ** $precision);
break;

case 'rounding-mode-up':
$round = fn(int|float $number, int $precision = 0) => ($number >= 0 ? ceil($number * (10 ** $precision)) : floor($number * (10 ** $precision))) / (10 ** $precision);
$round = fn(int|float|string $number, int $precision = 0) => ($number >= 0 ? ceil($number * (10 ** $precision)) : floor($number * (10 ** $precision))) / (10 ** $precision);
break;

case 'rounding-mode-down':
$round = fn(int|float $number, int $precision = 0) => ($number < 0 ? ceil($number * (10 ** $precision)) : floor($number * (10 ** $precision))) / (10 ** $precision);
$round = fn(int|float|string $number, int $precision = 0) => ($number < 0 ? ceil($number * (10 ** $precision)) : floor($number * (10 ** $precision))) / (10 ** $precision);
break;

case 'rounding-mode-half-even':
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, PHP_ROUND_HALF_EVEN);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, PHP_ROUND_HALF_EVEN);
break;

case 'rounding-mode-half-odd':
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, PHP_ROUND_HALF_ODD);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, PHP_ROUND_HALF_ODD);
break;

case 'rounding-mode-half-ceiling':
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, $number >= 0 ? PHP_ROUND_HALF_UP : PHP_ROUND_HALF_DOWN);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, $number >= 0 ? PHP_ROUND_HALF_UP : PHP_ROUND_HALF_DOWN);
break;

case 'rounding-mode-half-floor':
$rounding_mode = $number < 0 ? PHP_ROUND_HALF_UP : PHP_ROUND_HALF_DOWN;
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, $number < 0 ? PHP_ROUND_HALF_UP : PHP_ROUND_HALF_DOWN);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, $number < 0 ? PHP_ROUND_HALF_UP : PHP_ROUND_HALF_DOWN);
break;

case 'rounding-mode-half-down':
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, PHP_ROUND_HALF_DOWN);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, PHP_ROUND_HALF_DOWN);
break;

case 'rounding-mode-half-up':
$round = fn(int|float $number, int $precision = 0) => round($number, $precision, PHP_ROUND_HALF_UP);
$round = fn(int|float|string $number, int $precision = 0) => round($number + 0, $precision, PHP_ROUND_HALF_UP);
break;

case 'scale':
Expand Down
Loading