moneyphp / money

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

`roundToUnit` does not handles amounts less than 1 unit #740

Closed bendavies closed 1 year ago

bendavies commented 1 year ago

Hi there,

Given:

$currencies = new \Money\Currencies\ISOCurrencies();
$money = \Money\Money::USD(10); //10 cent

$money->roundToUnit($currencies->subunitFor($money->getCurrency()));

should return \Money\Money::USD(0)

but will throw:

Fatal error: Uncaught InvalidArgumentException: Leading zeros are not allowed in /vendor/moneyphp/money/src/Number.php:216
Stack trace:
#0 /vendor/moneyphp/money/src/Number.php(44): Money\Number::parseIntegerPart('000')
#1 /vendor/moneyphp/money/src/Number.php(53): Money\Number->__construct('000', '')
#2 /vendor/moneyphp/money/src/Money.php(81): Money\Number::fromString('000')
#3 /vendor/moneyphp/money/src/Money.php(410): Money\Money->__construct('000', Object(Money\Currency))
#4 /test.php(10): Money\Money->roundToUnit(2)
#5 {main}

Thanks

frederikbosch commented 1 year ago

I am not able to reproduce this. What should I change in PR #741?

bendavies commented 1 year ago

looking...

bendavies commented 1 year ago

@frederikbosch example updated from 1 cent to 10 cents.