-
Notifications
You must be signed in to change notification settings - Fork 448
Description
This is a fab library, so please excuse the potentially noob question. I've skimmed the code and tried to grok the changes over time but haven't yet found where this has been discussed...
If leading zeros are not allowed, why not just remove them when a string value is passed into Number::fromString()
and treat the fractional part verbatim?
Case in point: a shopping cart. A product on sale is 99p (GBP) so the normalized value passed in is 0.99
. The Money class accepts string|int values and silently converts them to smallest unit values via Number::fromString()
but only succeeds if the $amount
is 1 or above.
new Money('100', new Currency('GBP'));
=> 100 (£1)
new Money('99', new Currency('GBP'));
=> 99 (99p)
new Money('1.00', new Currency('GBP'));
=> 100 (£1)
new Money('0.99', new Currency('GBP'));
=> "Leading zeros are not allowed" exception
Why the inconsistency? It burdens the user, who may have their product prices stored in their databases as regular decimal values, to either convert them all to integers and strip off any leading zeros, or detect anything less than 1 and strip leading zeros from only those?
Could the Number class be altered to gracefully accept values in the range 0.01 => 0.99?
I see in previous versions of the code that ltrim($integerPart, '0')
has been dropped, so I can only assume it caused problems (and indeed, making this rudimentary change results in Empty number is invalid
because fromString()
removes any fractional part for reasons I can't grasp).
It's no biggie, as I've wrapped all calls to new Money()
with a helper function in my application that strips out decimal points and trims leading zeros, I was just curious if there was scope in the library to handle that bit for me to avoid this potential gotcha for numbers <1 :)