1
0
Fork 0
mirror of https://github.com/archtechx/money.git synced 2025-12-12 03:14:03 +00:00

Divide rate with 100

Needed for the calculation to be correct according to the documentation. `$money->addTax(20)` and not `$money->addTax(0.2)`
This commit is contained in:
Mikael Dalholm 2021-12-28 11:45:02 +01:00
parent 8ca21240df
commit 356aad9cc6
2 changed files with 6 additions and 6 deletions

View file

@ -115,7 +115,7 @@ final class Money implements JsonSerializable, Arrayable, Wireable
public function addFee(float $rate): self public function addFee(float $rate): self
{ {
return $this->multiplyBy( return $this->multiplyBy(
round(1 + $rate, $this->currency->mathDecimals()) round(1 + ($rate / 100), $this->currency->mathDecimals())
); );
} }
@ -129,7 +129,7 @@ final class Money implements JsonSerializable, Arrayable, Wireable
public function subtractFee(float $rate): self public function subtractFee(float $rate): self
{ {
return $this->divideBy( return $this->divideBy(
round(1 + $rate, $this->currency->mathDecimals()) round(1 + ($rate / 100), $this->currency->mathDecimals())
); );
} }

View file

@ -104,19 +104,19 @@ test('money can be divided', function () {
test('fees can be added to and subtracted from money', function () { test('fees can be added to and subtracted from money', function () {
$money = Money::fromDecimal(10.0); $money = Money::fromDecimal(10.0);
expect($money->addFee(0.1)->decimal())->toBe(11.0); expect($money->addFee(10)->decimal())->toBe(11.0);
expect($money->subtractFee(0.1)->decimal())->toBe(9.09); // 10/1.1 expect($money->subtractFee(10)->decimal())->toBe(9.09); // 10/1.1
}); });
test('taxes can be added and subtracted from money', function () { test('taxes can be added and subtracted from money', function () {
currencies()->add([CZK::class]); currencies()->add([CZK::class]);
expect( expect(
Money::fromDecimal(100.0, 'CZK')->addTax(0.21)->decimal() Money::fromDecimal(100.0, 'CZK')->addTax(21.0)->decimal()
)->toBe(121.0); )->toBe(121.0);
expect( expect(
Money::fromDecimal(121.0, 'CZK')->subtractTax(0.21)->decimal() Money::fromDecimal(121.0, 'CZK')->subtractTax(21.0)->decimal()
)->toBe(100.0); )->toBe(100.0);
}); });