Skip to content

Commit 863ce5f

Browse files
committed
Adds formatting string to defined decimal places
1 parent f9847fd commit 863ce5f

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/ElliotJReed/Maths/Number.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ public function __construct(self | int | float | string $number = 0, private rea
1717
$this->number = $this->castNumberToString($number);
1818
}
1919

20-
public function asString(): string
20+
public function asString(?int $decimalPlaces = null): string
2121
{
22+
if (null !== $decimalPlaces) {
23+
if ($decimalPlaces < 0) {
24+
throw new InvalidDecimalPlaces('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: ' . $decimalPlaces);
25+
}
26+
27+
return \number_format((float) $this->number, $decimalPlaces, '.', '');
28+
}
29+
2230
if (\str_contains($this->number, '.')) {
2331
$this->number = \rtrim($this->number, '0');
2432
}
@@ -36,7 +44,7 @@ public function asInteger(int $roundingMode = \PHP_ROUND_HALF_UP): int
3644
return (int) \round((float) $this->number, mode: $roundingMode);
3745
}
3846

39-
public function toDecimalPlaces(int $decimalPlaces, int $roundingMode = \PHP_ROUND_HALF_UP): self
47+
public function roundToDecimalPlaces(int $decimalPlaces, int $roundingMode = \PHP_ROUND_HALF_UP): self
4048
{
4149
if ($decimalPlaces < 0) {
4250
throw new InvalidDecimalPlaces('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: ' . $decimalPlaces);

tests/ElliotJReed/Maths/NumberTest.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@
1111

1212
final class NumberTest extends TestCase
1313
{
14+
public function testItReturnsNumberAsStringToDefinedDecimalPlaces(): void
15+
{
16+
$number = new Number(0.29533);
17+
18+
$this->assertSame('0.30', $number->asString(2));
19+
$this->assertSame('0.29533', $number->asString());
20+
$this->assertSame(0.29533, $number->asFloat());
21+
}
22+
23+
public function testItReturnsNumberAsWholeNumberStringToZeroDefinedDecimalPlaces(): void
24+
{
25+
$number = new Number(1.29533);
26+
27+
$this->assertSame('1', $number->asString(0));
28+
$this->assertSame('1.29533', $number->asString());
29+
$this->assertSame(1.29533, $number->asFloat());
30+
}
31+
32+
public function testItThrowsExceptionWhenDecimalPlacesArgumentIsLessThanZeroWhenReturningNumberAsAString(): void
33+
{
34+
$number = new Number('1.005');
35+
36+
$this->expectException(InvalidDecimalPlaces::class);
37+
$this->expectExceptionMessage('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: -2');
38+
39+
$number->asString(-2);
40+
}
41+
1442
public function testItMultipliesFloatingPointNumberAsFloat(): void
1543
{
1644
$number = new Number(0.295);
@@ -928,10 +956,10 @@ public function testItReturnsTrueWhenNumberIsEqualToTheBaseNumberWhenCheckingIfN
928956
$this->assertTrue($number->isGreaterThanOrEqualTo(new Number('1.002')));
929957
}
930958

931-
public function testItReturnsNumberSetToDefinedDecimalPlaces(): void
959+
public function testItReturnsNumberSetToDefinedDecimalPlacesWhenRoundingUpByDefault(): void
932960
{
933961
$number = new Number('1.005');
934-
$number->toDecimalPlaces(2);
962+
$number->roundToDecimalPlaces(2);
935963

936964
$this->assertSame('1.01', $number->asString());
937965
$this->assertSame(1.01, $number->asFloat());
@@ -942,21 +970,21 @@ public function testItReturnsNumberSetToDefinedDecimalPlaces(): void
942970
public function testItReturnsNumberSetToDefinedDecimalPlacesWhenRoundingDown(): void
943971
{
944972
$number = new Number('1.005');
945-
$number->toDecimalPlaces(2, \PHP_ROUND_HALF_DOWN);
973+
$number->roundToDecimalPlaces(2, \PHP_ROUND_HALF_DOWN);
946974

947975
$this->assertSame('1', $number->asString());
948976
$this->assertSame(1.0, $number->asFloat());
949977
$this->assertSame(1, $number->asInteger());
950978
$this->assertSame(1, $number->asInteger(\PHP_ROUND_HALF_DOWN));
951979
}
952980

953-
public function testItThrowsExceptionWhenDecimalPlacesArgumentIsLessThanZero(): void
981+
public function testItThrowsExceptionWhenDecimalPlacesArgumentIsLessThanZeroWhenRoundingNumber(): void
954982
{
955983
$number = new Number('1.005');
956984

957985
$this->expectException(InvalidDecimalPlaces::class);
958986
$this->expectExceptionMessage('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: -2');
959987

960-
$number->toDecimalPlaces(-2);
988+
$number->roundToDecimalPlaces(-2);
961989
}
962990
}

0 commit comments

Comments
 (0)