Skip to content

Commit 8864a03

Browse files
committed
abs method: fix validator
1 parent a02c3ef commit 8864a03

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/Simple/BCMath.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,15 @@ public function round($number, int $precision = 0): string
111111
*/
112112
public function abs($number): string
113113
{
114-
try {
115-
$this->add($number, 0); // validator
116-
if ($this->isNegative($number)) {
117-
return substr($number, 1);
118-
}
119-
120-
return $number;
121-
} catch (\Throwable $throwable) {
122-
// this is not a number
114+
if (!preg_match('~^-?\d+(\.\d+)?~', $number)) {
123115
return 0;
124116
}
117+
118+
if ($this->isNegative($number)) {
119+
return substr($number, 1);
120+
}
121+
122+
return $number;
125123
}
126124

127125
/**

0 commit comments

Comments
 (0)