Skip to content

Commit f9847fd

Browse files
committed
Adds decimal places method
1 parent f8a34e1 commit f9847fd

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ElliotJReed\Maths\Exception;
6+
7+
final class InvalidDecimalPlaces extends MathsException
8+
{
9+
protected $message = 'Decimal places must be a whole number greater than or equal to 0.';
10+
}

src/ElliotJReed/Maths/Number.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ElliotJReed\Maths;
66

7+
use ElliotJReed\Maths\Exception\InvalidDecimalPlaces;
78
use ElliotJReed\Maths\Exception\InvalidExponent;
89
use ElliotJReed\Maths\Exception\InvalidPowerModulusDivisor;
910

@@ -35,6 +36,17 @@ public function asInteger(int $roundingMode = \PHP_ROUND_HALF_UP): int
3536
return (int) \round((float) $this->number, mode: $roundingMode);
3637
}
3738

39+
public function toDecimalPlaces(int $decimalPlaces, int $roundingMode = \PHP_ROUND_HALF_UP): self
40+
{
41+
if ($decimalPlaces < 0) {
42+
throw new InvalidDecimalPlaces('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: ' . $decimalPlaces);
43+
}
44+
45+
$this->number = (string) \round((float) $this->number, $decimalPlaces, mode: $roundingMode);
46+
47+
return $this;
48+
}
49+
3850
public function add(self | int | float | string ...$number): self
3951
{
4052
foreach ($number as $numberAsIntegerOrFloatOrString) {

tests/ElliotJReed/Maths/NumberTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace ElliotJReed\Maths;
66

7+
use ElliotJReed\Maths\Exception\InvalidDecimalPlaces;
78
use ElliotJReed\Maths\Exception\InvalidExponent;
89
use ElliotJReed\Maths\Exception\InvalidPowerModulusDivisor;
910
use PHPUnit\Framework\TestCase;
@@ -926,4 +927,36 @@ public function testItReturnsTrueWhenNumberIsEqualToTheBaseNumberWhenCheckingIfN
926927

927928
$this->assertTrue($number->isGreaterThanOrEqualTo(new Number('1.002')));
928929
}
930+
931+
public function testItReturnsNumberSetToDefinedDecimalPlaces(): void
932+
{
933+
$number = new Number('1.005');
934+
$number->toDecimalPlaces(2);
935+
936+
$this->assertSame('1.01', $number->asString());
937+
$this->assertSame(1.01, $number->asFloat());
938+
$this->assertSame(1, $number->asInteger());
939+
$this->assertSame(1, $number->asInteger(\PHP_ROUND_HALF_DOWN));
940+
}
941+
942+
public function testItReturnsNumberSetToDefinedDecimalPlacesWhenRoundingDown(): void
943+
{
944+
$number = new Number('1.005');
945+
$number->toDecimalPlaces(2, \PHP_ROUND_HALF_DOWN);
946+
947+
$this->assertSame('1', $number->asString());
948+
$this->assertSame(1.0, $number->asFloat());
949+
$this->assertSame(1, $number->asInteger());
950+
$this->assertSame(1, $number->asInteger(\PHP_ROUND_HALF_DOWN));
951+
}
952+
953+
public function testItThrowsExceptionWhenDecimalPlacesArgumentIsLessThanZero(): void
954+
{
955+
$number = new Number('1.005');
956+
957+
$this->expectException(InvalidDecimalPlaces::class);
958+
$this->expectExceptionMessage('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: -2');
959+
960+
$number->toDecimalPlaces(-2);
961+
}
929962
}

0 commit comments

Comments
 (0)