diff --git a/src/RationalMoney.php b/src/RationalMoney.php index fd898f7..02a7de4 100644 --- a/src/RationalMoney.php +++ b/src/RationalMoney.php @@ -145,6 +145,16 @@ public function dividedBy($that) : RationalMoney return new self($amount, $this->currency); } + /** + * Returns a copy of this BigRational, with the amount simplified. + * + * @return RationalMoney + */ + public function simplified() : RationalMoney + { + return new self($this->amount->simplified(), $this->currency); + } + /** * @return string */ diff --git a/tests/RationalMoneyTest.php b/tests/RationalMoneyTest.php index 9a47ee2..f1cf35d 100644 --- a/tests/RationalMoneyTest.php +++ b/tests/RationalMoneyTest.php @@ -177,6 +177,33 @@ public function providerDividedBy() ]; } + /** + * @dataProvider providerSimplified + * + * @param array $rationalMoney + * @param string $expected + */ + public function testSimplified(array $rationalMoney, $expected) + { + $rationalMoney = RationalMoney::of(...$rationalMoney); + + $actual = $rationalMoney->simplified(); + $this->assertRationalMoneyEquals($expected, $actual); + } + + /** + * @return array + */ + public function providerSimplified() : array + { + return [ + [['123456/10000', 'USD'], 'USD 7716/625'], + [['695844/45600', 'CAD'], 'CAD 57987/3800'], + [['368022/405840', 'EUR'], 'EUR 61337/67640'], + [['-671244/45600', 'GBP'], 'GBP -55937/3800'], + ]; + } + /** * @dataProvider providerTo *