Download the PHP package ecommerce-utilities/money without Composer
On this page you can find all versions of the php package ecommerce-utilities/money. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package money
Money (PHP 8.1)
A small, precise money helper class for arithmetic operations and comparisons with configurable decimal precision. Uses bcmath for deterministic decimal calculations.
- Namespace:
Ecommerce - PHP: 8.1+
- Requires: PHP extension
ext-bcmath
Installation
- Ensure the bcmath PHP extension is enabled (e.g.,
sudo apt-get install php-bcmathand restart your web server/CLI). - Include the class in your project (e.g., via PSR-4 autoloading) and use the
Ecommercenamespace.
Quickstart
Controlling precision
- The default precision is 2 decimal places.
- You can set precision explicitly in several places:
- When creating:
Money::fromValue(123.456, 3) - In operations:
Money::add($a, $b, 3)
- When creating:
- During operations, precision is determined as follows:
1) The provided
$decimalPrecisionparameter (if set), 2) otherwise, the precision of aMoneyoperand (if present), 3) otherwise, 2.
Examples:
Rounding
Money::round($value, $decimalPrecision = null) rounds to the nearest value, with ties rounded away from zero (commercial rounding). It returns a new Money instance. Precision follows the same rules as other operations: explicit precision, otherwise the input Money precision, otherwise 2 decimal places.
Decimal values use bcround() when available (PHP 8.4+) and an equivalent BCMath fallback on PHP 8.1–8.3. Decimal strings are rounded and stored without conversion to float; use strings for values that require exact decimal precision. Scientific notation is also accepted. Float inputs retain PHP's float rounding behavior. Rounding an existing Money uses its already stored value; pass the original string when additional digits must be considered.
Null, string, and float inputs
All methods accept Money, int, float, numeric-string, or null (depending on the method):
Notes:
fromMoney($value)returns the numeric value (float) regardless of the input type.- The string representation is always formatted according to the set precision, e.g.,
"12.30".
Best practices
- Define a consistent precision for a domain (e.g., currency: 2, crypto assets: 8).
- For critical calculations, pass the precision explicitly to
add/sub/mul/div. - Use the comparison helpers instead of direct float comparisons.
API overview
- Creation:
Money::fromValue(null|Money|int|float|string $value, ?int $decimalPrecision = null): MoneyMoney::zero(): Money
- Arithmetic:
Money::add($a, $b, ?int $decimalPrecision = null): MoneyMoney::sub($a, $b, ?int $decimalPrecision = null): MoneyMoney::mul($a, $b, ?int $decimalPrecision = null): MoneyMoney::div($a, $b, ?int $decimalPrecision = null): MoneyMoney::round($value, ?int $decimalPrecision = null): Money
- Comparisons:
Money::isEqual($a, $b, ?int $decimalPrecision = null): boolMoney::isLeftSideGreater($a, $b, ?int $decimalPrecision = null): bool// true when a < bMoney::isRightSideGreater($a, $b, ?int $decimalPrecision = null): bool// true when a > b
- Access:
$money->getValue(): float$money->getDecimalPrecision(): int(string) $moneyfor formatted output
License
Feel free to use this class in your projects. Contributions and improvements are welcome.
All versions of money with dependencies
ext-bcmath Version *