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

addDecimal() and subtractDecimal()

This commit is contained in:
Samuel Štancl 2021-11-28 18:47:59 +01:00
parent b6b94e0530
commit c6ef5aa54c
2 changed files with 35 additions and 4 deletions

View file

@ -26,6 +26,12 @@ final class Money implements JsonSerializable, Arrayable, Wireable
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. */
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). */
public function subtract(int $value): self
{
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). */
public function subtractMoney(self $money): self
{
@ -250,11 +272,10 @@ final class Money implements JsonSerializable, Arrayable, Wireable
}
/** Get the cents from the decimal value. */
public function cents(): static
public function cents(): self
{
return static::fromDecimal(
$this->decimal() - floor($this->decimal()),
$this->currency,
return $this->newFromDecimal(
$this->decimal() - floor($this->decimal())
);
}