I decided to use the PHP library brick/money for currency calculations, since it's too imprecise to use floats, and it's annoying to convert amounts to cents so you use integers only, and to make easier dealing with rounding. I didn't go with the money PHP library because it's not flexible and demands inputs to be in integer-ish form (and yeah, I'm lazy to make input/output wrappers and don't want to be debugging in cents).
I'm noticing a tiny downside of using brick/money
since there isn't good documentation. I searched for a direct way to compare two amounts and this is a question I couldn't find an official/straightforward/direct form to do it.
So I have something like:
use Brick\Money\Money;
$money1 = Money::of(50, 'MXN'); // MXN 50.00
$money1 = $money1->plus('10'); // MXN 60.00 -- whatever operation
$money2 = Money::of(50, 'MXN'); // MXN 50.00
$money2 = $money1->minus('10'); // MXN 40.00 -- whatever operation
Question:
How can I compare the two amounts in an easier way? Do I need to call ->getMinorAmount()->toInt()
for each comparison?
Do I need to do something like the following for basic validation?:
if ($money1->getMinorAmount()->toInt() < 0 ||
$money1->getMinorAmount()->toInt() < $money2->getMinorAmount()->toInt()) {
$error = true;
}
Meanwhile in money PHP
you have functions like this:
$result = $value1->equals($value2);
$result = $value1->greaterThan($value2);
$result = $value1->greaterThanOrEqual($value2);
//etc...
Is this a missing feature or am I missing something? Thanks in advance.
There're similar methods in the AbstractMoney
class
$result = $money1->isEqualTo($money2);
$result = $money1->isGreaterThan($money2);
$result = $money1->isGreaterThanOrEqualTo($money2);
...