From 8c4183d130cde5e3e38f0734ec13dd244afc6924 Mon Sep 17 00:00:00 2001 From: Krystian Duma Date: Fri, 26 Apr 2024 10:43:35 +0200 Subject: [PATCH 1/2] Fix rounding error --- src/Values/Money.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Values/Money.php b/src/Values/Money.php index 5ca4ec0..9bfc6e5 100644 --- a/src/Values/Money.php +++ b/src/Values/Money.php @@ -11,7 +11,7 @@ public function __construct( public static function fromFloat(float $amount, int $decimals = 2): static { - return new static((int) ($amount * 10 ** $decimals), $decimals); + return new static(round($amount * 10 ** $decimals), $decimals); } public function __toString(): string @@ -23,4 +23,4 @@ public function toFloat(): float { return $this->amount / 10 ** $this->decimals; } -} \ No newline at end of file +} From 27af8a0a0e537d59542cafe72bb30f5cf96e21f0 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 26 Apr 2024 08:43:44 +0000 Subject: [PATCH 2/2] Restyled by php-cs-fixer --- src/Values/Money.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Values/Money.php b/src/Values/Money.php index 9bfc6e5..1873c04 100644 --- a/src/Values/Money.php +++ b/src/Values/Money.php @@ -7,18 +7,19 @@ class Money public function __construct( public readonly int $amount, public readonly int $decimals = 2, - ) { } - + ) { + } + public static function fromFloat(float $amount, int $decimals = 2): static { return new static(round($amount * 10 ** $decimals), $decimals); } - + public function __toString(): string { return number_format($this->toFloat(), $this->decimals, '.', ''); } - + public function toFloat(): float { return $this->amount / 10 ** $this->decimals;