moneyphp / money

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

Maybe I'm just an asshole, but this library does not appear to work with "money".... #624

Closed RickKukiela closed 3 years ago

RickKukiela commented 3 years ago

$money = Money::USD('123.12');

Throws an exception because the value passed is not an integer. I'm sorry but I thought $123.12 is a valid money value. I guess I'm just an asshole :(

kslimani commented 3 years ago

@RickKukiela the integer amount is always in the smallest currency unit. For USD smallest is the cent. So it should be something like :

$money = Money::USD('12312');

EDIT: take a look at https://moneyphp.org/en/stable/getting-started.html

RickKukiela commented 3 years ago

Yeah and then $money->getAmount() just spits out 12312.

If i have to manually convert my currency amounts to cents and then covert them manually back after I get the computed value from the object, then what is the point of this library? It literally is doing nothing at this pont.

kslimani commented 3 years ago

@RickKukiela alternatively you can parse decimal money amount string using Decimal parser : https://moneyphp.org/en/stable/features/parsing.html#decimal-parser

EDIT: should be something like :

use Money\Currency;
use Money\Currencies\ISOCurrencies;
use Money\Parser\DecimalMoneyParser;

$currencies = new ISOCurrencies();
$moneyParser = new DecimalMoneyParser($currencies);
$money = $moneyParser->parse('123.12', new Currency('USD'));
RickKukiela commented 3 years ago

IDK, I'm probably going to just look for a different library. It makes no sense to me that a library dedicated to handling money cannot just take a money value as an input. Its actually mind boggling.

UlrichEckhardt commented 3 years ago

Just because you don't understand something doesn't mean that it doesn't make sense. Let's leave this discussion closed unless all parties involved are actually interested in learning something.

RickKukiela commented 3 years ago

@UlrichEckhardt

Its not that I don't understand it. The reason I'm here looking for a library to handle this is specifically because I understand the issue and that money values should be handled as integers and not as floats. What I find ridiculous is that the library forces the developer using it to take all these unnecessary extra steps to fluff the values for the library. The developer then has to de-fluff them from the library after you get it back. Like why? Why should I have to do all these extra steps when the code to figure this out and convert the money value to something usable internally is the main draw to using a library like this.

If I have to manually convert every money value into cents, then manually convert the cents back into dollars after doing some math on it, then why not just do the math my self? I mean there is literally no point to this library if I have to do all the annoying heavy lifting my self.

Look at money.js for example, which is what I'm using on the front end. It does the something this library does, except, magically, it actually takes a value like "123.12" and coverts it to a string or integer or whatever internally and gives me a proper currency value back when I output it. Its crazy how useful it is.

Found a suitable replacement here: https://github.com/brick/money

Really dont get this library. Sorry.

Hesesses commented 2 years ago

Totally agree with @RickKukiela.

The reason I wanted to use the library was to be able to do $money = Money::USD('123.12');