Skip to content

Commit

Permalink
Merge pull request #6 from ulabox/issue/5
Browse files Browse the repository at this point in the history
fix(Money): fix issue #5
  • Loading branch information
acasademont authored May 18, 2017
2 parents c334864 + a1cf706 commit ae3e887
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ public function round($scale = 0)
if (!is_int($scale)) {
throw new InvalidArgumentException('Scale is not an integer');
}
$add = '0.' . str_repeat('0', $scale) . '5';
$newAmount = bcadd($this->amount, $add, $scale);
$newAmount = sprintf('%.'.$scale.'f', $this->amount());

return $this->newInstance($newAmount);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@ public function testRoundWithRounding()
$this->assertNotSame($money, $money->round());
}

/**
* @covers ::round
*/
public function testRoundWithNegativeAmountNoRounding()
{
$money = Money::fromAmount('-3.9813', Currency::fromCode('EUR'));
$expect = Money::fromAmount('-3.98', Currency::fromCode('EUR'));
$this->assertEquals($expect, $money->round(2));
}

/**
* @covers ::round
*/
public function testRoundWithNegativeAmountRounding()
{
$money = Money::fromAmount('-3.9863', Currency::fromCode('EUR'));
$expect = Money::fromAmount('-3.99', Currency::fromCode('EUR'));
$this->assertEquals($expect, $money->round(2));
}

/**
* @covers ::convertTo
*/
Expand Down

0 comments on commit ae3e887

Please sign in to comment.