diff --git a/README.md b/README.md index 0c7dd03..b16c36b 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,19 @@ $number->asString(); // '25' ```php use ElliotJReed\Maths\Number; +$number = new Number(10); +$anotherNumber = new Number($number); + +$number->asFloat(); // 10.0 +$anotherNumber->asFloat(); // 10.0 + +$number->add(10)->asFloat(); // 20.0 +$anotherNumber->add(5)->asFloat(); // 15.0 +``` + +```php +use ElliotJReed\Maths\Number; + $number = new Number(1.125); $number->roundToDecimalPlaces(2); diff --git a/src/ElliotJReed/Maths/NumberFormat.php b/src/ElliotJReed/Maths/NumberFormat.php index a8343a2..55a5c8e 100644 --- a/src/ElliotJReed/Maths/NumberFormat.php +++ b/src/ElliotJReed/Maths/NumberFormat.php @@ -12,13 +12,15 @@ abstract class NumberFormat protected string $number; /** - * @param int|float|string $number (Optional) The "base" number. Default: 0 - * @param int $precision (Optional) The number of digits after the decimal place in the result. Default: 24 + * @param \ElliotJReed\Maths\Number|\ElliotJReed\Maths\NumberImmutable|int|float|string $number (Optional) The "base" number. Default: 0 + * @param int $precision (Optional) The number of digits after the decimal place in the result. Default: 24 * * @throws \ElliotJReed\Maths\Exception\NonNumericValue thrown when number argument is not numeric */ - public function __construct(int | float | string $number = 0, protected readonly int $precision = 24) - { + public function __construct( + NumberImmutable | Number | int | float | string $number = 0, + protected readonly int $precision = 24 + ) { $this->number = $this->castNumberToString($number); } diff --git a/tests/ElliotJReed/Maths/NumberImmutableTest.php b/tests/ElliotJReed/Maths/NumberImmutableTest.php index 48da4aa..de0e02e 100644 --- a/tests/ElliotJReed/Maths/NumberImmutableTest.php +++ b/tests/ElliotJReed/Maths/NumberImmutableTest.php @@ -9,6 +9,7 @@ use ElliotJReed\Maths\Exception\InvalidExponent; use ElliotJReed\Maths\Exception\InvalidPowerModulusDivisor; use ElliotJReed\Maths\Exception\NonNumericValue; +use ElliotJReed\Maths\Number; use ElliotJReed\Maths\NumberImmutable; use PHPUnit\Framework\TestCase; @@ -42,6 +43,24 @@ public function testItReturnsNumberWhenNegativeBaseNumberIsInScientificNotation( $this->assertSame(0, $number->asInteger(\PHP_ROUND_HALF_DOWN)); } + public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumberImmutable(): void + { + $number = new NumberImmutable(new NumberImmutable(10000.29533)); + + $this->assertSame('10000.30', $number->asString(2)); + $this->assertSame('10000.29533', $number->asString()); + $this->assertSame(10000.29533, $number->asFloat()); + } + + public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumber(): void + { + $number = new NumberImmutable(new Number(10000.29533)); + + $this->assertSame('10000.30', $number->asString(2)); + $this->assertSame('10000.29533', $number->asString()); + $this->assertSame(10000.29533, $number->asFloat()); + } + public function testItReturnsNumberAsStringToDefinedDecimalPlaces(): void { $number = new NumberImmutable(10000.29533); diff --git a/tests/ElliotJReed/Maths/NumberTest.php b/tests/ElliotJReed/Maths/NumberTest.php index eeaa061..ac3a4d3 100644 --- a/tests/ElliotJReed/Maths/NumberTest.php +++ b/tests/ElliotJReed/Maths/NumberTest.php @@ -10,6 +10,7 @@ use ElliotJReed\Maths\Exception\InvalidPowerModulusDivisor; use ElliotJReed\Maths\Exception\NonNumericValue; use ElliotJReed\Maths\Number; +use ElliotJReed\Maths\NumberImmutable; use PHPUnit\Framework\TestCase; final class NumberTest extends TestCase @@ -42,6 +43,24 @@ public function testItReturnsNumberWhenNegativeBaseNumberIsInScientificNotation( $this->assertSame(0, $number->asInteger(\PHP_ROUND_HALF_DOWN)); } + public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumber(): void + { + $number = new Number(new Number(10000.29533)); + + $this->assertSame('10000.30', $number->asString(2)); + $this->assertSame('10000.29533', $number->asString()); + $this->assertSame(10000.29533, $number->asFloat()); + } + + public function testItReturnsNumberWhenBaseNumberIsInstantiatedUsingInstanceOfNumberImmutable(): void + { + $number = new Number(new NumberImmutable(10000.29533)); + + $this->assertSame('10000.30', $number->asString(2)); + $this->assertSame('10000.29533', $number->asString()); + $this->assertSame(10000.29533, $number->asFloat()); + } + public function testItReturnsNumberAsStringToDefinedDecimalPlaces(): void { $number = new Number(10000.29533);