Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasgeniar committed May 9, 2020
1 parent 63b11d3 commit 126ad68
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ composer require mattiasgeniar/php-percentages
```php
use Mattiasgeniar\Percentage\Percentage;

// Percentage increase/decrease from $a to $b
Percentage::changeBetween(100, 120); // 20%
// Percentage increase 100 to 120?
Percentage::changeBetween(100, 120); // 20%

// How much is $a in relation to $b?
Percentage::percentageOf(120, 100); // 120%
Percentage::percentageOf(50, 100); // 50%
// What's the absolute change from 100 to 80?
Percentage::absoluteChangeBetween(100, 80); // 20%, not -20%

// What is $a percentage of $b?
Percentage::fromNumber(20, 200); // 40
Percentage::fromNumber(50, 200); // 100
// How much is 120 to 100?
Percentage::percentageOf(120, 100); // 120%

// How much is 50 to 100?
Percentage::percentageOf(50, 100); // 50%

// What is 20% of 200?
Percentage::fromNumber(20, 200); // 40

// What is the 140% extension from 3 to 2?
Percentage::extension(140, 3, 2); // 1.6

// What is the 140% extension from 1 to 2?
Percentage::extension(140, 1, 2); // 2.4
```

## Testing
Expand Down
34 changes: 27 additions & 7 deletions src/Percentage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,53 @@

class Percentage
{
public function __construct()
/*
What is the percentage increase or decrease
from $a to $b ?
*/
public static function changeBetween($a, $b)
{
// constructor body
return floatval(($b - $a) / $a * 100);
}

/*
What is the percentage increase or decrease
What is the absolute percentage increase or decrease
from $a to $b ?
*/
public static function changeBetween($a, $b)
public static function absoluteChangeBetween($a, $b)
{
return ($b - $a) / $a * 100;
return floatval(abs(static::changeBetween($a, $b)));
}

/*
How much is $a of $b in percentages?
*/
public static function percentageOf($a, $b)
{
return $a * 100 / $b;
return floatval($a * 100 / $b);
}

/*
Get a percentage return from a number.
*/
public static function fromNumber($percentage, $number)
{
return $number * ($percentage / 100);
return floatval($number * ($percentage / 100));
}

/*
Get the absolute value if you extend range $a to $b with $percentage
*/
public static function extension($percentage, $a, $b): float
{
if ($a > $b) {
$movement = $a - $b;

return floatval($a - ($movement * $percentage / 100));
} else {
$movement = $b - $a;

return floatval($a + ($movement * $percentage / 100));
}
}
}
10 changes: 10 additions & 0 deletions tests/PercentageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function it_can_calculate_simple_percentage_differences()
$this->assertEquals(20, Percentage::changeBetween(100, 120));

$this->assertEquals(-20, Percentage::changeBetween(100, 80));

$this->assertEquals(20, Percentage::absoluteChangeBetween(100, 80));
}

/** @test */
Expand All @@ -38,4 +40,12 @@ public function it_can_get_the_percentage_value_of_a_number()

$this->assertEquals(-40, Percentage::fromNumber(-20, 200));
}

/** @test */
public function it_can_get_the_percentage_extension_value()
{
$this->assertEquals(1.6, Percentage::extension(140, 3, 2));

$this->assertEquals(2.4, Percentage::extension(140, 1, 2));
}
}

0 comments on commit 126ad68

Please sign in to comment.