mirror of
https://github.com/archtechx/money.git
synced 2025-12-12 03:14:03 +00:00
addDecimal() and subtractDecimal()
This commit is contained in:
parent
b6b94e0530
commit
c6ef5aa54c
2 changed files with 35 additions and 4 deletions
|
|
@ -26,6 +26,12 @@ final class Money implements JsonSerializable, Arrayable, Wireable
|
||||||
return new self($value, $this->currency);
|
return new self($value, $this->currency);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create a new Money instance with the same currency from a decimal value. */
|
||||||
|
protected function newFromDecimal(float $decimal): self
|
||||||
|
{
|
||||||
|
return static::fromDecimal($decimal, $this->currency);
|
||||||
|
}
|
||||||
|
|
||||||
/** Create a Money instance from a decimal value. */
|
/** Create a Money instance from a decimal value. */
|
||||||
public static function fromDecimal(float $decimal, Currency|string $currency = null): self
|
public static function fromDecimal(float $decimal, Currency|string $currency = null): self
|
||||||
{
|
{
|
||||||
|
|
@ -49,12 +55,28 @@ final class Money implements JsonSerializable, Arrayable, Wireable
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Add money (in decimal value). */
|
||||||
|
public function addDecimal(float $decimal): self
|
||||||
|
{
|
||||||
|
return $this->addMoney(
|
||||||
|
$this->newFromDecimal($decimal)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/** Subtract money (in base value). */
|
/** Subtract money (in base value). */
|
||||||
public function subtract(int $value): self
|
public function subtract(int $value): self
|
||||||
{
|
{
|
||||||
return $this->new($this->value - $value);
|
return $this->new($this->value - $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Subtract money (in decimal value). */
|
||||||
|
public function subtractDecimal(float $decimal): self
|
||||||
|
{
|
||||||
|
return $this->subtractMoney(
|
||||||
|
$this->newFromDecimal($decimal)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/** Subtract money (of another Money instance). */
|
/** Subtract money (of another Money instance). */
|
||||||
public function subtractMoney(self $money): self
|
public function subtractMoney(self $money): self
|
||||||
{
|
{
|
||||||
|
|
@ -250,11 +272,10 @@ final class Money implements JsonSerializable, Arrayable, Wireable
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the cents from the decimal value. */
|
/** Get the cents from the decimal value. */
|
||||||
public function cents(): static
|
public function cents(): self
|
||||||
{
|
{
|
||||||
return static::fromDecimal(
|
return $this->newFromDecimal(
|
||||||
$this->decimal() - floor($this->decimal()),
|
$this->decimal() - floor($this->decimal())
|
||||||
$this->currency,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -225,6 +225,16 @@ test('the cents from the decimal value can be fetched using the cents method', f
|
||||||
expect(money(123456789, CZK::class)->cents()->value())->toBe(89);
|
expect(money(123456789, CZK::class)->cents()->value())->toBe(89);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('decimal values can be added and subtracted', function () {
|
||||||
|
expect(
|
||||||
|
money(1234)->addDecimal(21.3)->value()
|
||||||
|
)->toBe(3364);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
money(1234)->subtractDecimal(8.3)->value()
|
||||||
|
)->toBe(404);
|
||||||
|
});
|
||||||
|
|
||||||
test('money can be serialized to JSON', function () {
|
test('money can be serialized to JSON', function () {
|
||||||
currencies()->add(CZK::class);
|
currencies()->add(CZK::class);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue