Skip to content

Commit

Permalink
Using intl to format numbers produced by Number::toReadableSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 31, 2014
1 parent 012d482 commit 0abef1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Utility/Number.php
Expand Up @@ -111,15 +111,15 @@ public static function precision($value, $precision = 3) {
public static function toReadableSize($size) {
switch (true) {
case $size < 1024:
return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
return __dn('cake', '{0,number,integer} Byte', '{0,number,integer} Bytes', $size, $size);
case round($size / 1024) < 1024:
return __d('cake', '%s KB', static::precision($size / 1024, 0));
return __d('cake', '{0,number,#,###.##} KB', $size / 1024);
case round($size / 1024 / 1024, 2) < 1024:
return __d('cake', '%s MB', static::precision($size / 1024 / 1024, 2));
return __d('cake', '{0,number,#,###.##} MB', $size / 1024 / 1024);
case round($size / 1024 / 1024 / 1024, 2) < 1024:
return __d('cake', '%s GB', static::precision($size / 1024 / 1024 / 1024, 2));
return __d('cake', '{0,number,#,###.##} GB', $size / 1024 / 1024 / 1024);
default:
return __d('cake', '%s TB', static::precision($size / 1024 / 1024 / 1024 / 1024, 2));
return __d('cake', '{0,number} TB', $size / 1024 / 1024 / 1024 / 1024);
}
}

Expand Down
24 changes: 14 additions & 10 deletions tests/TestCase/Utility/NumberTest.php
Expand Up @@ -615,47 +615,51 @@ public function testToReadableSize() {
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1023);
$expected = '1023 Bytes';
$expected = '1,023 Bytes';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024);
$expected = '1 KB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 + 123);
$expected = '1.12 KB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 512);
$expected = '512 KB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 - 1);
$expected = '1.00 MB';
$expected = '1 MB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 512);
$expected = '512.00 MB';
$result = $this->Number->toReadableSize(512.05 * 1024 * 1024);
$expected = '512.05 MB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 - 1);
$expected = '1.00 GB';
$expected = '1 GB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 512);
$expected = '512.00 GB';
$expected = '512 GB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 - 1);
$expected = '1.00 TB';
$expected = '1 TB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 512);
$expected = '512.00 TB';
$expected = '512 TB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 - 1);
$expected = '1024.00 TB';
$expected = '1,024 TB';
$this->assertEquals($expected, $result);

$result = $this->Number->toReadableSize(1024 * 1024 * 1024 * 1024 * 1024 * 1024);
$expected = (1024 * 1024) . '.00 TB';
$expected = '1,048,576 TB';
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit 0abef1a

Please sign in to comment.