Skip to content

Commit

Permalink
multiply and divide with different locale (#361)
Browse files Browse the repository at this point in the history
* multiply and divide with different locale
  • Loading branch information
frederikbosch committed Mar 20, 2017
1 parent fd1c642 commit 79a7de6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Unversioned

### Fixed

- Multiplying and dividing with a locale that use commas as separator

## 3.0.2 - 2017-03-11

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ public function multiply($multiplier, $roundingMode = self::ROUND_HALF_UP)
$this->assertOperand($multiplier);
$this->assertRoundingMode($roundingMode);

if (is_float($multiplier)) {
$multiplier = (string) Number::fromFloat($multiplier);
}

$product = $this->round($this->getCalculator()->multiply($this->amount, $multiplier), $roundingMode);

return $this->newInstance($product);
Expand All @@ -325,6 +329,10 @@ public function divide($divisor, $roundingMode = self::ROUND_HALF_UP)
$this->assertOperand($divisor);
$this->assertRoundingMode($roundingMode);

if (is_float($divisor)) {
$divisor = (string) Number::fromFloat($divisor);
}

if ($this->getCalculator()->compare((string) $divisor, '0') === 0) {
throw new \InvalidArgumentException('Division by zero');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static function fromFloat($floatingPoint)
throw new \InvalidArgumentException('Floating point expected');
}

return self::fromString(sprintf('%.8g', $floatingPoint));
return self::fromString(sprintf('%.8F', $floatingPoint));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ public function it_multiplies_the_amount($multiplier, $roundingMode, $result)
$this->assertEquals((string) $result, $money->getAmount());
}

/**
* @test
*/
public function it_multiplies_the_amount_with_locale_that_uses_comma_separator()
{
setlocale(LC_ALL, 'es_ES.utf8');

$money = new Money(100, new Currency(self::CURRENCY));
$money = $money->multiply(10 / 100);

$this->assertInstanceOf(Money::class, $money);
$this->assertEquals(10, $money->getAmount());

setlocale(LC_ALL, null);
}

/**
* @dataProvider invalidOperandExamples
* @expectedException \InvalidArgumentException
Expand Down

0 comments on commit 79a7de6

Please sign in to comment.