Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiply and divide with different locale #361

Merged
merged 3 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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