mirror of
https://github.com/archtechx/money.git
synced 2025-12-12 11:24:03 +00:00
cents() method
This commit is contained in:
parent
8dc5287868
commit
f5bf658155
2 changed files with 24 additions and 0 deletions
|
|
@ -249,6 +249,15 @@ final class Money implements JsonSerializable, Arrayable, Wireable
|
|||
return $this->rounded()->value() - $this->value();
|
||||
}
|
||||
|
||||
/** Get the cents from the decimal value. */
|
||||
public function cents(): static
|
||||
{
|
||||
return static::fromDecimal(
|
||||
$this->decimal() - floor($this->decimal()),
|
||||
$this->currency,
|
||||
);
|
||||
}
|
||||
|
||||
public function toLivewire()
|
||||
{
|
||||
return $this->toArray();
|
||||
|
|
|
|||
|
|
@ -210,6 +210,21 @@ test('the is method compares both the value and the currency', function () {
|
|||
)->toBeFalse();
|
||||
});
|
||||
|
||||
test('the cents from the decimal value can be fetched using the cents method', function () {
|
||||
currencies()->add(CZK::class);
|
||||
|
||||
expect(money(100)->cents())->toBeInstanceOf(Money::class);
|
||||
|
||||
expect(money(1234, USD::class)->cents()->value())->toBe(34);
|
||||
expect(money(1234, CZK::class)->cents()->value())->toBe(34);
|
||||
|
||||
expect(money(100, USD::class)->cents()->value())->toBe(0);
|
||||
expect(money(100, CZK::class)->cents()->value())->toBe(0);
|
||||
|
||||
expect(money(123456789, USD::class)->cents()->value())->toBe(89);
|
||||
expect(money(123456789, CZK::class)->cents()->value())->toBe(89);
|
||||
});
|
||||
|
||||
test('money can be serialized to JSON', function () {
|
||||
currencies()->add(CZK::class);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue