diff --git a/README.md b/README.md index 89d55fa..f9046f7 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,8 @@ echo Number::fromRoman("CXXV"); // 125 **Binary Suffix** +Convert a number of bytes in to the highest applicable data unit + ```php use Coduo\PHPHumanizer\Number; @@ -128,13 +130,22 @@ use Coduo\PHPHumanizer\Number; echo Number::binarySuffix(1536, 'pl'); // "1,5 kB" ``` -Number can also be humanized with a specific number of decimal places by providing a precision parameter (between 0 and 3) +Number can also be humanized with a specific number of decimal places with ```preciseBinarySuffix($number, $precision, $locale = 'en')``` +The precision parameter must be between 0 and 3. + +```php +use Coduo\PHPHumanizer\Number; + +echo Number::preciseBinarySuffix(1024, 2); // "1.00 kB" +echo Number::binarySuffix(1325899906842624, 3); // "1.178 PB" +``` + +This function also supports locale ```php use Coduo\PHPHumanizer\Number; -echo Number::binarySuffix(1024, 'en', 3); // "1.000 kB" -echo Number::binarySuffix(1325899906842624, 'en', 3); // "1.178 PB" +echo Number::binarySuffix(1325899906842624, 3, 'pl); // "1,178 PB" ``` **Metric Suffix** diff --git a/src/Coduo/PHPHumanizer/Number.php b/src/Coduo/PHPHumanizer/Number.php index f6877da..89caf87 100644 --- a/src/Coduo/PHPHumanizer/Number.php +++ b/src/Coduo/PHPHumanizer/Number.php @@ -19,7 +19,14 @@ public static function ordinal($number) return (string) new Ordinal($number); } - public static function binarySuffix($number, $locale = 'en', $precision = null) + public static function binarySuffix($number, $locale = 'en') + { + $binarySuffix = new BinarySuffix($number, $locale); + + return $binarySuffix->convert(); + } + + public static function preciseBinarySuffix($number, $precision, $locale = 'en') { $binarySuffix = new BinarySuffix($number, $locale, $precision); diff --git a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php index 38c178f..c2ef873 100644 --- a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php @@ -42,16 +42,16 @@ public function test_convert_number_to_string_with_binary_suffix($expected, $num } /** - * @dataProvider binarySuffixWithPrecisionDataProvider + * @dataProvider preciseBinarySuffixDataProvider * * @param $expected * @param $number * @param string $locale * @param integer $precision */ - public function test_convert_number_to_string_with_binary_suffix_precision($expected, $number, $locale = 'en', $precision) + public function test_convert_number_to_string_with_precise_binary_suffix($expected, $number, $precision, $locale = 'en') { - $this->assertEquals($expected, Number::binarySuffix($number, $locale, $precision)); + $this->assertEquals($expected, Number::preciseBinarySuffix($number, $precision, $locale)); } /** @@ -179,22 +179,22 @@ public function binarySuffixDataProvider() /** * @return array */ - public function binarySuffixWithPrecisionDataProvider() + public function preciseBinarySuffixDataProvider() { return array( - array(-1, -1, 'en', 3), - array("0 bytes", 0, 'en', 3), - array("1 bytes", 1, 'en', 3), - array("1.000 kB", 1024, 'en', 3), - array("1.001 kB", 1025, 'en', 3), - array("1.500 kB", 1536, 'en', 3), - array("5.000 MB", 1048576 * 5, 'en', 3), - array("2.000 GB", 1073741824 * 2, 'en', 3), - array("3.000 TB", 1099511627776 * 3, 'en', 3), - array("1.178 PB", 1325899906842624, 'en', 3), + array(-1, -1, 3), + array("0 bytes", 0, 3), + array("1 bytes", 1, 3), + array("1.000 kB", 1024, 3), + array("1.001 kB", 1025, 3), + array("1.500 kB", 1536, 3), + array("5.000 MB", 1048576 * 5, 3), + array("2.000 GB", 1073741824 * 2, 3), + array("3.000 TB", 1099511627776 * 3, 3), + array("1.178 PB", 1325899906842624, 3), - array("1,500 kB", 1536, 'pl', 3), - array("1,178 PB", 1325899906842624, 'pl', 3), + array("1,500 kB", 1536, 3, 'pl'), + array("1,178 PB", 1325899906842624, 3, 'pl'), ); }