moneyphp / money

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

Subtract and add not using scale #507

Closed dzona closed 5 years ago

dzona commented 6 years ago

BcMathCalculator add and subtract methods ignore scale set up for calculator, thus giving wrong results.

$total = '0.00125148';
$fee = '0.00005006';
$calc = new BcMathCalculator($bitCoinScale = 8);
$amount = $calc->subtract($total, $fee);
var_dump($amount); // string(1) "0"
bmeynell commented 6 years ago

@dzona - I'm curious about this too - did you come to any conclusions? 0 is clearly hard-coded for add() and subtract() in lieu of $this->scale:

Add: https://github.com/moneyphp/money/blob/8655b0f62763acb893bad53be35323af25ad557c/src/Calculator/BcMathCalculator.php#L46-L49

Subtract: https://github.com/moneyphp/money/blob/8655b0f62763acb893bad53be35323af25ad557c/src/Calculator/BcMathCalculator.php#L59-L62

🤔

tonydspaniard commented 6 years ago

Scale is a must for any operation to provide proper results. Not sure but could also be #509 related