Skip to content

Commit 83e77dd

Browse files
committed
fix validator
1 parent 8864a03 commit 83e77dd

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Simple/BCMath.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,13 @@ public function round($number, int $precision = 0): string
111111
*/
112112
public function abs($number): string
113113
{
114-
if (!preg_match('~^-?\d+(\.\d+)?~', $number)) {
114+
if (!preg_match('~^-?\d*(\.\d*)?$~', $number, $matches)) {
115+
return 0;
116+
}
117+
118+
$digits = $matches[0] ?? '0';
119+
$division = $matches[1] ?? '.';
120+
if ($digits === '.' && $division === '.') {
115121
return 0;
116122
}
117123

tests/MathTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testAbs(string $class): void
2222
$provider = app($class);
2323

2424
// not number
25+
$this->assertEquals($provider->abs('.'), 0);
2526
$this->assertEquals($provider->abs('hello'), 0);
2627
$this->assertEquals($provider->abs('--121'), 0);
2728
$this->assertEquals($provider->abs('---121'), 0);
@@ -31,10 +32,14 @@ public function testAbs(string $class): void
3132
$this->assertEquals($provider->abs(-123), 123);
3233

3334
// float
35+
$this->assertEquals($provider->abs(.0), 0);
36+
$this->assertEquals($provider->abs(123.0), 123);
3437
$this->assertEquals($provider->abs(123.11), 123.11);
3538
$this->assertEquals($provider->abs(-123.11), 123.11);
3639

3740
// string
41+
$this->assertEquals($provider->abs('123.'), 123);
42+
$this->assertEquals($provider->abs('.11'), .11);
3843
$this->assertEquals($provider->abs('123.11'), 123.11);
3944
$this->assertEquals($provider->abs('-123.11'), 123.11);
4045
}

0 commit comments

Comments
 (0)