mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Fixing issue with rounded values in NumberHelper::toReadableSize. Fixes #5676.
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7790 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
ee8ff1d5ac
commit
41d82920f5
2 changed files with 54 additions and 31 deletions
|
@ -64,13 +64,13 @@ class NumberHelper extends AppHelper {
|
|||
return '1 Byte';
|
||||
case $size < 1024:
|
||||
return $size . ' Bytes';
|
||||
case $size < 1024 * 1024:
|
||||
case round($size / 1024) < 1024:
|
||||
return $this->precision($size / 1024, 0) . ' KB';
|
||||
case $size < 1024 * 1024 * 1024:
|
||||
case round($size / 1024 / 1024, 2) < 1024:
|
||||
return $this->precision($size / 1024 / 1024, 2) . ' MB';
|
||||
case $size < 1024 * 1024 * 1024 * 1024:
|
||||
case round($size / 1024 / 1024 / 1024, 2) < 1024:
|
||||
return $this->precision($size / 1024 / 1024 / 1024, 2) . ' GB';
|
||||
case $size < 1024 * 1024 * 1024 * 1024 * 1024:
|
||||
default:
|
||||
return $this->precision($size / 1024 / 1024 / 1024 / 1024, 2) . ' TB';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -287,29 +287,52 @@ class NumberTest extends CakeTestCase {
|
|||
$expected = '1 Byte';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$under1KB = 45;
|
||||
$result = $this->Number->toReadableSize($under1KB);
|
||||
$expected = $under1KB.' Bytes';
|
||||
$result = $this->Number->toReadableSize(45);
|
||||
$expected = '45 Bytes';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$under1MB = 1024*1024-1;
|
||||
$result = $this->Number->toReadableSize($under1MB);
|
||||
$expected = sprintf("%01.0f", $under1MB/1024).' KB';
|
||||
$result = $this->Number->toReadableSize(1023);
|
||||
$expected = '1023 Bytes';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$under1GB = (float) 1024*1024*1024-1;
|
||||
$result = $this->Number->toReadableSize($under1GB);
|
||||
$expected = sprintf("%01.2f", $under1GB/1024/1024).' MB';
|
||||
$result = $this->Number->toReadableSize(1024);
|
||||
$expected = '1 KB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$float = (float) 1024*1024*1024*1023-1;
|
||||
$result = $this->Number->toReadableSize($float);
|
||||
$expected = sprintf("%01.2f", $float/1024/1024/1024).' GB';
|
||||
$result = $this->Number->toReadableSize(1024*512);
|
||||
$expected = '512 KB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$float = (float) 1024*1024*1024*1024*1023-1;
|
||||
$result = $this->Number->toReadableSize($float);
|
||||
$expected = sprintf("%01.2f", $float/1024/1024/1024/1024).' TB';
|
||||
$result = $this->Number->toReadableSize(1024*1024-1);
|
||||
$expected = '1.00 MB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*512);
|
||||
$expected = '512.00 MB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*1024-1);
|
||||
$expected = '1.00 GB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*1024*512);
|
||||
$expected = '512.00 GB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*1024*1024-1);
|
||||
$expected = '1.00 TB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*1024*1024*512);
|
||||
$expected = '512.00 TB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*1024*1024*1024-1);
|
||||
$expected = '1024.00 TB';
|
||||
$this->assertEqual($expected, $result);
|
||||
|
||||
$result = $this->Number->toReadableSize(1024*1024*1024*1024*1024*1024);
|
||||
$expected = (1024 * 1024) . '.00 TB';
|
||||
$this->assertEqual($expected, $result);
|
||||
}
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue