Skip to content

Commit

Permalink
moved optional precision parameter of binarySuffix in to its own func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
mostertb committed Nov 3, 2015
1 parent 90d69ce commit fa73af3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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**
Expand Down
9 changes: 8 additions & 1 deletion src/Coduo/PHPHumanizer/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
32 changes: 16 additions & 16 deletions tests/Coduo/PHPHumanizer/Tests/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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'),
);
}

Expand Down

0 comments on commit fa73af3

Please sign in to comment.