11
11
12
12
final class NumberTest extends TestCase
13
13
{
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
+
14
42
public function testItMultipliesFloatingPointNumberAsFloat (): void
15
43
{
16
44
$ number = new Number (0.295 );
@@ -928,10 +956,10 @@ public function testItReturnsTrueWhenNumberIsEqualToTheBaseNumberWhenCheckingIfN
928
956
$ this ->assertTrue ($ number ->isGreaterThanOrEqualTo (new Number ('1.002 ' )));
929
957
}
930
958
931
- public function testItReturnsNumberSetToDefinedDecimalPlaces (): void
959
+ public function testItReturnsNumberSetToDefinedDecimalPlacesWhenRoundingUpByDefault (): void
932
960
{
933
961
$ number = new Number ('1.005 ' );
934
- $ number ->toDecimalPlaces (2 );
962
+ $ number ->roundToDecimalPlaces (2 );
935
963
936
964
$ this ->assertSame ('1.01 ' , $ number ->asString ());
937
965
$ this ->assertSame (1.01 , $ number ->asFloat ());
@@ -942,21 +970,21 @@ public function testItReturnsNumberSetToDefinedDecimalPlaces(): void
942
970
public function testItReturnsNumberSetToDefinedDecimalPlacesWhenRoundingDown (): void
943
971
{
944
972
$ number = new Number ('1.005 ' );
945
- $ number ->toDecimalPlaces (2 , \PHP_ROUND_HALF_DOWN );
973
+ $ number ->roundToDecimalPlaces (2 , \PHP_ROUND_HALF_DOWN );
946
974
947
975
$ this ->assertSame ('1 ' , $ number ->asString ());
948
976
$ this ->assertSame (1.0 , $ number ->asFloat ());
949
977
$ this ->assertSame (1 , $ number ->asInteger ());
950
978
$ this ->assertSame (1 , $ number ->asInteger (\PHP_ROUND_HALF_DOWN ));
951
979
}
952
980
953
- public function testItThrowsExceptionWhenDecimalPlacesArgumentIsLessThanZero (): void
981
+ public function testItThrowsExceptionWhenDecimalPlacesArgumentIsLessThanZeroWhenRoundingNumber (): void
954
982
{
955
983
$ number = new Number ('1.005 ' );
956
984
957
985
$ this ->expectException (InvalidDecimalPlaces::class);
958
986
$ this ->expectExceptionMessage ('Decimal places must be a whole number greater than or equal to 0. Invalid decimal places number: -2 ' );
959
987
960
- $ number ->toDecimalPlaces (-2 );
988
+ $ number ->roundToDecimalPlaces (-2 );
961
989
}
962
990
}
0 commit comments