TellerΒΆ
Legacy codebases often use float math for monetary calculations, which leads to problems with fractions-of-pennies in monetary amounts. The proper solution is to introduce a Money object, and use Money objects in place of float math. However, doing so can be quite an onerous task, especially when the float values need to be moved to and from database storage; intercepting and coercing the float values (often represented by strings) can be very difficult and time-consuming.
To help ease the transition from float math to Money objects, use a Teller instance to replace float math for monetary calculations in place:
// before
$price = 234.56;
$discount = 0.05;
$discountAmount = $price * $discount; // 11.728
// after
$teller = \Money\Teller::USD();
$discountAmount = $teller->multiply($price, $discount); // '11.73'
The main drawback is that you cannot use two different currencies with the Teller; you can use only one.
The Teller offers these methods:
operation
absolute($amount) : stringReturns an absolute monetary amount.add($amount, $other, ...$others) : stringAdds one or more monetary amounts to a monetary amount.divide($amount, $divisor) : stringDivides a monetary amount by a divisor.mod($amount, $divisor)Returns the mod of one amount by another.multiply($amount, $multiplier) : stringMultiplies a monetary amount by a multiplier.negative($amount) : stringNegates a monetary amount.ratioOf($amount, $other)Determines the ratio of one monetary amount to another.subtract($amount, $other, ...$others) : stringSubtracts one or more monetary amounts from a monetary amount.
comparison
compare($amount, $other) : intCompares one monetary amount to the other; -1 is less than, 0 is equals, 1 is greater than.equals($amount, $other) : boolAre two monetary amounts equal?greaterThan($amount, $other) : boolIs one monetary amount greater than the other?greaterThanOrEqual($amount, $other) : boolIs one monetary amount greater than or equal to the other?isNegative($amount) : boolIs a monetary amount less than zero?isPositive($amount) : boolIs a monetary amount greater than zero?isZero($amount) : boolIs a monetary amount equal to zero?lessThan($amount, $other) : boolIs one monetary amount less than the other?lessThanOrEqual($amount, $other) : boolIs one monetary amount less than or equal to the other?
allocation
allocate($amount, array $ratios) : string[]Allocates a monetary amount according to an array of ratios.allocateTo($amount, $n) : string[]Allocates a monetary amount among N targets.
aggregation
avg($amount, ...$amounts) : stringAverages a series of monetary amounts.sum($amount, ...$amounts) : stringSums a series of monetary amounts.max($amount, ...$amounts) : stringFinds the highest of a series of monetary amounts.min($amount, ...$amounts) : stringFinds the lowest of a series of monetary amounts.
conversion
convertToMoney($amount) : MoneyConverts a monetary amount to a Money object.convertToMoneyArray(array $amounts) : MoneyConverts an array of monetary amounts to an array of Money objects.convertToString($amount) : stringConverts a monetary amount to a string.convertToStringArray($amount) : stringConverts an array of monetary amounts to an array of strings.zero() : stringReturns a zero monetary amount ('0.00').