moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.61k stars 440 forks source link

Divisions throws InvalidArgumentException "Invalid fractional part" when using GmpCalculator #538

Closed alesak closed 4 years ago

alesak commented 5 years ago
$value = new Money('500', new Currency('USD'));
$p = $value->divide(110); //throws exception
alesak commented 5 years ago

PHP 5.6 MoneyPHP version 3.2.0

frederikbosch commented 5 years ago

How about php7+?

alesak commented 5 years ago

How about php7+?

Haven't tested it, we are still forced to run it on 5.6 as lots of our older projects are not fully compatible with PHP7

frederikbosch commented 5 years ago

Can you add a failing test as PR? Then I will fix it.

frederikbosch commented 5 years ago

I cannot replicate this with PHP 5.6

alesak commented 5 years ago

Sorry, it looks like the value has to be -500:

$value = new Money('-500', new Currency('USD'));
$p = $value->divide(110); //throws exception

/vendor/moneyphp/money/src/Number.php on line 287
/vendor/moneyphp/money/src/Number.php(38): Money\Number::parseFractionalPart('-54545454545455')
/vendor/moneyphp/money/src/Number.php(56): Money\Number->__construct('-4', '-54545454545455')
/vendor/moneyphp/money/src/Number.php(89): Money\Number::fromString('-4.-54545454545...')
/vendor/moneyphp/money/src/Calculator/GmpCalculator.php(191): Money\Number::fromNumber('-4.-54545454545...')
/vendor/moneyphp/money/src/Money.php(456): Money\Calculator\GmpCalculator->round('-4.-54545454545...', 1)
/vendor/moneyphp/money/src/Money.php(338): Money\Money->round('-4.-54545454545...', 1)
default.php(55): Money\Money->divide(110)

PHPCalculator & BcMathCalculator works

alesak commented 5 years ago

Can you add a failing test as PR? Then I will fix it.

I will try to do so but I am not sure when I can get the chance

frederikbosch commented 5 years ago

@alesak I already created the PR.

pmjones commented 5 years ago

@frederikbosch thanks for looking into this!

I'd like to add that the problem with the GMP calculator occurs in multiply() as well; e.g., if you multiply -0.09 times 0.01 you get InvalidArgumentException: Invalid fractional part -9. Invalid digit - found.

frederikbosch commented 5 years ago

@pmjones Is your bug resolved with PR #546? Maybe it is time to merge it then.

pmjones commented 5 years ago

My coworker informs me that is it not resolved by #546; I presume because the PR applies to divide() but not multiply(). (We do have a workaround in place.)