Skip to content

Commit

Permalink
Divide rate with 100
Browse files Browse the repository at this point in the history
Needed for the calculation to be correct according to the documentation. `$money->addTax(20)` and not `$money->addTax(0.2)`
  • Loading branch information
dalholm committed Dec 28, 2021
1 parent 8ca2124 commit 356aad9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function divideBy(float $number): self
public function addFee(float $rate): self
{
return $this->multiplyBy(
round(1 + $rate, $this->currency->mathDecimals())
round(1 + ($rate / 100), $this->currency->mathDecimals())
);
}

Expand All @@ -129,7 +129,7 @@ public function addTax(float $rate): self
public function subtractFee(float $rate): self
{
return $this->divideBy(
round(1 + $rate, $this->currency->mathDecimals())
round(1 + ($rate / 100), $this->currency->mathDecimals())
);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Pest/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@
test('fees can be added to and subtracted from money', function () {
$money = Money::fromDecimal(10.0);

expect($money->addFee(0.1)->decimal())->toBe(11.0);
expect($money->subtractFee(0.1)->decimal())->toBe(9.09); // 10/1.1
expect($money->addFee(10)->decimal())->toBe(11.0);
expect($money->subtractFee(10)->decimal())->toBe(9.09); // 10/1.1
});

test('taxes can be added and subtracted from money', function () {
currencies()->add([CZK::class]);

expect(
Money::fromDecimal(100.0, 'CZK')->addTax(0.21)->decimal()
Money::fromDecimal(100.0, 'CZK')->addTax(21.0)->decimal()
)->toBe(121.0);

expect(
Money::fromDecimal(121.0, 'CZK')->subtractTax(0.21)->decimal()
Money::fromDecimal(121.0, 'CZK')->subtractTax(21.0)->decimal()
)->toBe(100.0);
});

Expand Down

0 comments on commit 356aad9

Please sign in to comment.