[core] only show decimal for KB/MB/etc less than 10

This commit is contained in:
Shish 2024-01-08 19:35:25 +00:00
parent 7135eca9c2
commit 29fde1388a
3 changed files with 15 additions and 12 deletions

View file

@ -506,17 +506,17 @@ function to_shorthand_int(int $int): string
{
assert($int >= 0);
if ($int >= pow(1024, 4)) {
return sprintf("%.1fTB", $int / pow(1024, 4));
} elseif ($int >= pow(1024, 3)) {
return sprintf("%.1fGB", $int / pow(1024, 3));
} elseif ($int >= pow(1024, 2)) {
return sprintf("%.1fMB", $int / pow(1024, 2));
} elseif ($int >= 1024) {
return sprintf("%.1fKB", $int / 1024);
} else {
return (string)$int;
}
return match (true) {
$int >= pow(1024, 4) * 10 => sprintf("%.0fTB", $int / pow(1024, 4)),
$int >= pow(1024, 4) => sprintf("%.1fTB", $int / pow(1024, 4)),
$int >= pow(1024, 3) * 10 => sprintf("%.0fGB", $int / pow(1024, 3)),
$int >= pow(1024, 3) => sprintf("%.1fGB", $int / pow(1024, 3)),
$int >= pow(1024, 2) * 10 => sprintf("%.0fMB", $int / pow(1024, 2)),
$int >= pow(1024, 2) => sprintf("%.1fMB", $int / pow(1024, 2)),
$int >= pow(1024, 1) * 10 => sprintf("%.0fKB", $int / pow(1024, 1)),
$int >= pow(1024, 1) => sprintf("%.1fKB", $int / pow(1024, 1)),
default => (string)$int,
};
}
abstract class TIME_UNITS
{

View file

@ -93,7 +93,10 @@ class PolyfillsTest extends TestCase
public function test_to_shorthand_int()
{
// 0-9 should have 1 decimal place, 10+ should have none
$this->assertEquals("1.1GB", to_shorthand_int(1231231231));
$this->assertEquals("10KB", to_shorthand_int(10240));
$this->assertEquals("9.2KB", to_shorthand_int(9440));
$this->assertEquals("2", to_shorthand_int(2));
}

View file

@ -30,7 +30,7 @@ class ViewPostTest extends ShimmiePHPUnitTestCase
$config->set_string(ImageConfig::INFO, '$size // $filesize // $ext');
$this->get_page("post/view/$image_id_1");
$this->assert_text("640x480 // 19.3KB // jpg");
$this->assert_text("640x480 // 19KB // jpg");
}
public function testPrevNext()