@@ -19,9 +19,9 @@ final class Calculator
1919 */
2020 public function add (int |float |string $ a , int |float |string $ b ) : int |float
2121 {
22- $ result = BigDecimal::of ($ a )->plus (BigDecimal::of ($ b ));
22+ $ result = BigDecimal::of (( string ) $ a )->plus (BigDecimal::of (( string ) $ b ));
2323
24- if (\in_array ( \rtrim ( $ result ->getFractionalPart (), ' 0 ' ), [ ' 0 ' , '' ], true )) {
24+ if (! $ result ->hasNonZeroFractionalPart ( )) {
2525 return $ result ->toInt ();
2626 }
2727
@@ -39,34 +39,29 @@ public function divide(int|float|string $a, int|float|string $b, ?int $scale = n
3939 {
4040 try {
4141 if ($ scale === null && $ rounding === null ) {
42- $ result = BigDecimal::of ($ a )->exactlyDividedBy (BigDecimal::of ($ b ));
42+ $ result = BigDecimal::of ($ a )->dividedByExact (BigDecimal::of ($ b ));
4343
44- if (\in_array ( \rtrim ( $ result ->getFractionalPart (), ' 0 ' ), [ ' 0 ' , '' ], true )) {
44+ if (! $ result ->hasNonZeroFractionalPart ( )) {
4545 return $ result ->toInt ();
4646 }
4747 }
4848
49- if ($ rounding === null ) {
50- $ brickMode = RoundingMode::UNNECESSARY ;
51- } else {
52-
53- $ brickMode = match ($ rounding ) {
54- Rounding::UNNECESSARY => RoundingMode::UNNECESSARY ,
55- Rounding::UP => RoundingMode::UP ,
56- Rounding::DOWN => RoundingMode::DOWN ,
57- Rounding::CEILING => RoundingMode::CEILING ,
58- Rounding::FLOOR => RoundingMode::FLOOR ,
59- Rounding::HALF_UP => RoundingMode::HALF_UP ,
60- Rounding::HALF_DOWN => RoundingMode::HALF_DOWN ,
61- Rounding::HALF_CEILING => RoundingMode::HALF_CEILING ,
62- Rounding::HALF_FLOOR => RoundingMode::HALF_FLOOR ,
63- Rounding::HALF_EVEN => RoundingMode::HALF_EVEN ,
64- };
65- }
66-
67- $ result = BigDecimal::of ($ a )->dividedBy (BigDecimal::of ($ b ), $ scale , $ brickMode );
68-
69- if (\in_array (\rtrim ($ result ->getFractionalPart (), '0 ' ), ['0 ' , '' ], true )) {
49+ $ brickMode = match ($ rounding ) {
50+ Rounding::UP => RoundingMode::Up,
51+ Rounding::DOWN => RoundingMode::Down,
52+ Rounding::CEILING => RoundingMode::Ceiling,
53+ Rounding::FLOOR => RoundingMode::Floor,
54+ Rounding::HALF_UP => RoundingMode::HalfUp,
55+ Rounding::HALF_DOWN => RoundingMode::HalfDown,
56+ Rounding::HALF_CEILING => RoundingMode::HalfCeiling,
57+ Rounding::HALF_FLOOR => RoundingMode::HalfFloor,
58+ Rounding::HALF_EVEN => RoundingMode::HalfEven,
59+ default => RoundingMode::Unnecessary,
60+ };
61+
62+ $ result = BigDecimal::of ((string ) $ a )->dividedBy (BigDecimal::of ((string ) $ b ), $ scale , $ brickMode );
63+
64+ if (!$ result ->hasNonZeroFractionalPart ()) {
7065 return $ result ->toInt ();
7166 }
7267
@@ -93,9 +88,9 @@ public function modulus(int|string $a, int|string $b) : int
9388 */
9489 public function multiply (int |float |string $ a , int |float |string $ b ) : int |float
9590 {
96- $ result = BigDecimal::of ($ a )->multipliedBy (BigDecimal::of ($ b ));
91+ $ result = BigDecimal::of (( string ) $ a )->multipliedBy (BigDecimal::of (( string ) $ b ));
9792
98- if (\in_array ( \rtrim ( $ result ->getFractionalPart (), ' 0 ' ), [ ' 0 ' , '' ], true )) {
93+ if (! $ result ->hasNonZeroFractionalPart ( )) {
9994 return $ result ->toInt ();
10095 }
10196
@@ -108,9 +103,9 @@ public function multiply(int|float|string $a, int|float|string $b) : int|float
108103 */
109104 public function power (int |float |string $ a , int |string $ b ) : int |float
110105 {
111- $ result = BigDecimal::of ($ a )->power (BigInteger::of ($ b )->toInt ());
106+ $ result = BigDecimal::of (( string ) $ a )->power (BigInteger::of (( string ) $ b )->toInt ());
112107
113- if (\in_array ( $ result ->getFractionalPart (), [ ' 0 ' , '' ], true )) {
108+ if (! $ result ->hasNonZeroFractionalPart ( )) {
114109 return $ result ->toInt ();
115110 }
116111
@@ -123,9 +118,9 @@ public function power(int|float|string $a, int|string $b) : int|float
123118 */
124119 public function subtract (int |float |string $ a , int |float |string $ b ) : int |float
125120 {
126- $ result = BigDecimal::of ($ a )->minus (BigDecimal::of ($ b ));
121+ $ result = BigDecimal::of (( string ) $ a )->minus (BigDecimal::of (( string ) $ b ));
127122
128- if (\in_array ( \rtrim ( $ result ->getFractionalPart (), ' 0 ' ), [ ' 0 ' , '' ], true )) {
123+ if (! $ result ->hasNonZeroFractionalPart ( )) {
129124 return $ result ->toInt ();
130125 }
131126
0 commit comments