Browse Source

Using intl to format numbers produced by Number::toReadableSize()

Jose Lorenzo Rodriguez 11 years ago
parent
commit
0abef1ac82
2 changed files with 19 additions and 15 deletions
  1. 5 5
      src/Utility/Number.php
  2. 14 10
      tests/TestCase/Utility/NumberTest.php

+ 5 - 5
src/Utility/Number.php

@@ -111,15 +111,15 @@ class Number {
 	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);
 		}
 	}
 

+ 14 - 10
tests/TestCase/Utility/NumberTest.php

@@ -615,47 +615,51 @@ class NumberTest extends TestCase {
 		$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);
 	}