@@ -21,7 +21,7 @@ public function add(int|float|string $a, int|float|string $b) : int|float
2121 {
2222 $ result = BigDecimal::of ((string ) $ a )->plus (BigDecimal::of ((string ) $ b ));
2323
24- if (!$ result -> hasNonZeroFractionalPart ()) {
24+ if (!self :: hasNonZeroFractionalPart ($ result )) {
2525 return $ result ->toInt ();
2626 }
2727
@@ -39,9 +39,10 @@ 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 )->dividedBy (BigDecimal::of ($ b ));
42+ $ aDecimal = BigDecimal::of ((string ) $ a );
43+ $ result = $ aDecimal ->dividedBy (BigDecimal::of ((string ) $ b ), $ aDecimal ->getScale ());
4344
44- if (!$ result -> hasNonZeroFractionalPart ()) {
45+ if (!self :: hasNonZeroFractionalPart ($ result )) {
4546 return $ result ->toInt ();
4647 }
4748 }
@@ -63,7 +64,7 @@ public function divide(int|float|string $a, int|float|string $b, ?int $scale = n
6364
6465 $ result = BigDecimal::of ((string ) $ a )->dividedBy (BigDecimal::of ((string ) $ b ), $ scale , $ brickMode );
6566
66- if (!$ result -> hasNonZeroFractionalPart ()) {
67+ if (!self :: hasNonZeroFractionalPart ($ result )) {
6768 return $ result ->toInt ();
6869 }
6970
@@ -92,7 +93,7 @@ public function multiply(int|float|string $a, int|float|string $b) : int|float
9293 {
9394 $ result = BigDecimal::of ((string ) $ a )->multipliedBy (BigDecimal::of ((string ) $ b ));
9495
95- if (!$ result -> hasNonZeroFractionalPart ()) {
96+ if (!self :: hasNonZeroFractionalPart ($ result )) {
9697 return $ result ->toInt ();
9798 }
9899
@@ -107,7 +108,7 @@ public function power(int|float|string $a, int|string $b) : int|float
107108 {
108109 $ result = BigDecimal::of ((string ) $ a )->power (BigInteger::of ((string ) $ b )->toInt ());
109110
110- if (!$ result -> hasNonZeroFractionalPart ()) {
111+ if (!self :: hasNonZeroFractionalPart ($ result )) {
111112 return $ result ->toInt ();
112113 }
113114
@@ -122,10 +123,19 @@ public function subtract(int|float|string $a, int|float|string $b) : int|float
122123 {
123124 $ result = BigDecimal::of ((string ) $ a )->minus (BigDecimal::of ((string ) $ b ));
124125
125- if (!$ result -> hasNonZeroFractionalPart ()) {
126+ if (!self :: hasNonZeroFractionalPart ($ result )) {
126127 return $ result ->toInt ();
127128 }
128129
129130 return $ result ->toFloat ();
130131 }
132+
133+ private static function hasNonZeroFractionalPart (BigDecimal $ result ) : bool
134+ {
135+ if (method_exists ($ result , 'hasNonZeroFractionalPart ' )) {
136+ return $ result ->hasNonZeroFractionalPart ();
137+ }
138+
139+ return !$ result ->getFractionalPart ()->isZero ();
140+ }
131141}
0 commit comments