Skip to content

Commit

Permalink
Metric suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Apr 25, 2014
1 parent d7fd45f commit fd51e91
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ use PHPHumanizer\Number;
echo Number::binarySuffix(1536, 'pl'); "1,5 kB"
```

**Metric Suffix**

```php
use PHPHumanizer\Number;

echo Number::metricSuffix(-1); // "-1"
echo Number::metricSuffix(0); // "0"
echo Number::metricSuffix(1); // "1"
echo Number::metricSuffix(101); // "101"
echo Number::metricSuffix(1000); // "1k"
echo Number::metricSuffix(1240); // "1.2k"
echo Number::metricSuffix(1240000); // "1.24M"
echo Number::metricSuffix(3500000); // "3.5M"
```

Number can be also formatted for specific locale

```php
use PHPHumanizer\Number;

echo Number::metricSuffix(1240000, 'pl'); "1,24M"
```

# Credits

This lib was inspired by [Java Humanize Lib](https://github.com/mfornos/humanize) && [Rails Active Support](https://github.com/rails/rails/tree/master/activesupport/lib/active_support)
27 changes: 26 additions & 1 deletion spec/PHPHumanizer/NumberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,32 @@ function it_convert_number_to_string_with_binary_suffix_for_specific_locale()

function it_throw_exception_when_converting_to_string_with_binary_suffix_non_numeric_values()
{
$this->shouldThrow(new \RuntimeException("binarySuffix converter accept only numeric values."))
$this->shouldThrow(new \InvalidArgumentException("Binary suffix converter accept only numeric values."))
->during('binarySuffix', array('as12'));
}

function it_convert_number_to_string_with_metric_suffix()
{
$this->metricSuffix(-1)->shouldReturn("-1");
$this->metricSuffix(0)->shouldReturn("0");
$this->metricSuffix(1)->shouldReturn("1");
$this->metricSuffix(101)->shouldReturn("101");
$this->metricSuffix(1000)->shouldReturn("1k");
$this->metricSuffix(1240)->shouldReturn("1.2k");
$this->metricSuffix(1240000)->shouldReturn("1.24M");
$this->metricSuffix(3500000)->shouldReturn("3.5M");
}

function it_convert_number_to_string_with_metric_suffix_for_specific_locale()
{
$this->metricSuffix(1240, 'pl')->shouldReturn("1,2k");
$this->metricSuffix(1240000, 'pl')->shouldReturn("1,24M");
$this->metricSuffix(3500000, 'pl')->shouldReturn("3,5M");
}

function it_throw_exception_when_converting_to_string_with_metric_suffix_non_numeric_values()
{
$this->shouldThrow(new \InvalidArgumentException("Metric suffix converter accept only numeric values."))
->during('metricSuffix', array('as12'));
}
}
7 changes: 7 additions & 0 deletions src/PHPHumanizer/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPHumanizer\Number\Ordinal;
use PHPHumanizer\String\BinarySuffix;
use PHPHumanizer\String\MetricSuffix;

class Number
{
Expand All @@ -22,4 +23,10 @@ public static function binarySuffix($number, $locale = 'en')
$binarySuffix = new BinarySuffix($number, $locale);
return $binarySuffix->convert();
}

public static function metricSuffix($number, $locale = 'en')
{
$binarySuffix = new MetricSuffix($number, $locale);
return $binarySuffix->convert();
}
}
4 changes: 2 additions & 2 deletions src/PHPHumanizer/String/BinarySuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class BinarySuffix
/**
* @param $number
* @param string $locale
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function __construct($number, $locale = 'en')
{
if (!is_numeric($number)) {
throw new \RuntimeException("binarySuffix converter accept only numeric values.");
throw new \InvalidArgumentException("Binary suffix converter accept only numeric values.");
}

$this->number = (int) $number;
Expand Down
61 changes: 61 additions & 0 deletions src/PHPHumanizer/String/MetricSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace PHPHumanizer\String;

class MetricSuffix
{
const CONVERT_THRESHOLD = 1000;

/**
* @var int
*/
private $number;

/**
* @var string
*/
private $locale;

/**
* @var array
*/
private $binaryPrefixes = array(
1000000000000000 => '#.##P',
1000000000000 => '#.##T',
1000000000 => '#.##G',
1000000 => '#.##M',
1000 => '#.#k',
0 => '#.#'
);

/**
* @param $number
* @param string $locale
* @throws \InvalidArgumentException
*/
public function __construct($number, $locale = 'en')
{
if (!is_numeric($number)) {
throw new \InvalidArgumentException("Metric suffix converter accept only numeric values.");
}

$this->number = (int) $number;
$this->locale = $locale;
}

public function convert()
{
$formatter = new \NumberFormatter($this->locale, \NumberFormatter::PATTERN_DECIMAL);

foreach ($this->binaryPrefixes as $size => $unitPattern) {
if ($size <= $this->number) {
$value = ($this->number >= self::CONVERT_THRESHOLD) ? $this->number / (double) $size : $this->number;
$formatter->setPattern($unitPattern);

return $formatter->format($value);
}
}

return $formatter->format($this->number);
}
}

0 comments on commit fd51e91

Please sign in to comment.