Skip to content

Commit

Permalink
BC Math Calculator: add scale to add and subtract operations (#528)
Browse files Browse the repository at this point in the history
* BC Math Calculator: add scale to add and subtract operations

* add test for subtract

* add string cast

* style
  • Loading branch information
frederikbosch committed Dec 6, 2018
1 parent 665fe6e commit 42e55ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Calculator/BcMathCalculator.php
Expand Up @@ -45,7 +45,7 @@ public function compare($a, $b)
*/
public function add($amount, $addend)
{
return bcadd($amount, $addend, 0);
return (string) Number::fromString(bcadd($amount, $addend, $this->scale));
}

/**
Expand All @@ -58,7 +58,7 @@ public function add($amount, $addend)
*/
public function subtract($amount, $subtrahend)
{
return bcsub($amount, $subtrahend, 0);
return (string) Number::fromString(bcsub($amount, $subtrahend, $this->scale));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Calculator/BcMathCalculatorTest.php
Expand Up @@ -56,4 +56,20 @@ public function it_compares_numbers_close_to_zero()
$this->assertEquals(1, $this->getCalculator()->compare('1', '0.0005'));
$this->assertEquals(1, $this->getCalculator()->compare('1', '0.000000000000000000000000005'));
}

/**
* @test
*/
public function it_uses_scale_for_add()
{
$this->assertEquals('0.00130154000000', $this->getCalculator()->add('0.00125148', '0.00005006'));
}

/**
* @test
*/
public function it_uses_scale_for_subtract()
{
$this->assertEquals('0.00120142', $this->getCalculator()->subtract('0.00125148', '0.00005006'));
}
}

0 comments on commit 42e55ab

Please sign in to comment.