Skip to content

Commit

Permalink
fix abs
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Mar 11, 2020
1 parent 7ebee2b commit fb69e12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Simple/BCMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,17 @@ public function round($number, int $precision = 0): string
*/
public function abs($number): string
{
if ($this->isNegative($number)) {
return substr($number, 1);
}
try {
$this->add($number, 0); // validator
if ($this->isNegative($number)) {
return substr($number, 1);
}

return $number;
return $number;
} catch (\Throwable $throwable) {
// this is not a number
return 0;
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function testAbs(string $class): void
*/
$provider = app($class);

// not number
$this->assertEquals($provider->abs('hello'), 0);
$this->assertEquals($provider->abs('--121'), 0);
$this->assertEquals($provider->abs('---121'), 0);

// int
$this->assertEquals($provider->abs(123), 123);
$this->assertEquals($provider->abs(-123), 123);
Expand Down

0 comments on commit fb69e12

Please sign in to comment.