Comparison

A number of built in methods are available for comparing Money objects.

Same Currency

isSameCurrency() compares whether two or more Money objects have the same currency.

$value1 = Money::USD(800);                      // $8.00
$value2 = Money::USD(100);                      // $1.00
$value3 = Money::EUR(800);                      // €8.00

$result = $value1->isSameCurrency($value2);    // true
$result = $value1->isSameCurrency($value3);    // false

isSameCurrency() also accepts variadic arguments so that you can test whether all instances in a collection have the same currency as the target or not.

$target = Money::USD(800);
$containsEuros = [
    Money::USD(500),
    Money::EUR(800)
];
$allDollars = [
    Money::USD(500),
    Money::USD(600),
];
$result = $target->isSameCurrency(...$containsEuros); // false
$result = $target->isSameCurrency(...$allDollars);    // true

Equality

equals() compares whether two Money objects are equal in value and currency.

$value1 = Money::USD(800);              // $8.00
$value2 = Money::USD(800);              // $8.00
$value3 = Money::EUR(800);              // €8.00

$result = $value1->equals($value2);     // true
$result = $value1->equals($value3);     // false

Compare

compare() returns an integer indicating whether the first Money object is less than, equal to, or greater than the second. Both Money objects must have the same currency.

$value1 = Money::USD(200);                  // $2.00
$value2 = Money::USD(400);                  // $4.00
$value3 = Money::USD(400);                  // $4.00

$result = $value1->compare($value2);        // -1, less than
$result = $value2->compare($value3);        // 0, equals to
$result = $value2->compare($value1);        // 1, more than

// Both Money objects must have the same currency, otherwise
// an InvalidArgumentException will be thrown:
$value4 = Money::EUR(100);
$result = $value1->compare($value4);        // throws InvalidArgumentException

Greater Than

greaterThan() compares whether the first Money object is larger than the second.

$value1 = Money::USD(800);                  // $8.00
$value2 = Money::USD(700);                  // $7.00

$result = $value1->greaterThan($value2);    // true

You can also use greaterThanOrEqual() to additionally check for equality.

$value1 = Money::USD(800);                          // $8.00
$value2 = Money::USD(800);                          // $8.00

$result = $value1->greaterThanOrEqual($value2);     // true

Less Than

lessThan() compares whether the first Money object is less than the second.

$value1 = Money::USD(800);              // $8.00
$value2 = Money::USD(700);              // $7.00

$result = $value1->lessThan($value2);   // false

You can also use lessThanOrEqual() to additionally check for equality.

$value1 = Money::USD(800);                      // $8.00
$value2 = Money::USD(800);                      // $8.00

$result = $value1->lessThanOrEqual($value2);    // true

Value Sign

You may determine the sign of Money object using the following methods.

  • isZero()

  • isPositive()

  • isNegative()

Money::USD(100)->isZero();          // false
Money::USD(0)->isZero();            // true
Money::USD(-100)->isZero();         // false

Money::USD(100)->isPositive();      // true
Money::USD(0)->isPositive();        // false
Money::USD(-100)->isPositive();     // false

Money::USD(100)->isNegative();      // false
Money::USD(0)->isNegative();        // false
Money::USD(-100)->isNegative();     // true