Money Value Objects & Arithmetic
Totoglu\Currency\Value\Money is a final readonly value object backed by integer minor units (int $amountMinor), an uppercase 3-letter ISO 4217 code (string $currency), and $minorUnits (0..8, e.g., 2 for EUR/USD, 0 for JPY).
Constructing Money
Section titled “Constructing Money”<?php namespace ProcessWire;
use Totoglu\Currency\Domain\RoundingMode;use Totoglu\Currency\Value\Money;
// From minor units (integer cents)$deposit = Money::fromMinor(490000, 'EUR');
// From major string or integer (rejects float to prevent IEEE-754 drift)$charter = Money::fromMajor('14700.00', 'EUR', minorUnits: 2, roundingMode: RoundingMode::HalfUp);
// Zero value or module convenience helper (auto-resolves managed minor_units)$zero = Money::zero('EUR');$managed = Currency::requireInstance($wire)->money('14700.00', 'EUR');Exact Operations, Allocation & Cash Rounding
Section titled “Exact Operations, Allocation & Cash Rounding”Every arithmetic method returns a new immutable Money instance and throws \InvalidArgumentException if currencies or minorUnits differ:
<?php namespace ProcessWire;
use Totoglu\Currency\Domain\RoundingMode;use Totoglu\Currency\Value\Money;
$charter = Money::fromMajor('14700.00', 'EUR');$apa = $charter->percentage('30', RoundingMode::HalfUp); // 4410.00 EUR$subtotal = $charter->add($apa); // 19110.00 EUR$discounted = $subtotal->subtract(Money::fromMajor('110.00', 'EUR'));$twoWeeks = $discounted->multiply('2', RoundingMode::HalfEven);
// Largest-remainder allocation guarantees sum($parts) === $subtotal->amountMinor[$deposit, $balance] = $subtotal->allocate([30, 70]);
// Cash rounding to nearest 5.00 CHF (500 minor units) or 0.05 CHF (5 minor units)$cashChf = Money::fromMajor('199.97', 'CHF')->applyIncrement(5, RoundingMode::HalfUp); // 199.95 CHF
// Sign & comparison helpers$credit = $deposit->negate();$positive = $credit->abs();$isGreater = $balance->compare($deposit) > 0;$isEqual = $deposit->equals(Money::fromMajor('5733.00', 'EUR'));$majorString = $deposit->toMajorString(); // '5733.00'Five RoundingMode Strategies
Section titled “Five RoundingMode Strategies”| Case | String Value | Behavior |
|---|---|---|
RoundingMode::HalfUp |
half_up |
Standard commercial rounding (ties round away from zero). |
RoundingMode::HalfEven |
half_even |
Banker’s rounding (ties round to nearest even digit). |
RoundingMode::HalfDown |
half_down |
Ties round toward zero. |
RoundingMode::Ceil |
ceil |
Always rounds toward positive infinity. |
RoundingMode::Floor |
floor |
Always rounds toward negative infinity. |