brick/math
brick/math is a PHP library for arbitrary precision arithmetic. It provides BigInteger, BigDecimal, BigRational, and BigNumber classes, automatically using GMP or BCMath when available for fast, exact calculations.
Build vs. buy for money and quantity math. Adopt brick/math as the arithmetic primitive instead of building arbitrary-precision decimal/integer/fraction handling in-house. It is MIT-licensed, Composer-installable, and provides BigInteger, BigDecimal, and BigRational with immutable value objects — a low-cost foundation for billing, ledger, pricing, tax, payout, and usage-metering features.
Feature scope for financial correctness. Use it when a product decision depends on exact arithmetic rather than IEEE-754 floats: proration, multi-line invoices, tax breakdowns, FX conversion, discount allocation, refunds, payroll deductions, loyalty points, prepaid credits, crypto/token amounts, or large IDs. The package makes rounding an explicit product policy via RoundingMode instead of an accidental side effect.
Rounding and allocation policy as a product requirement. BigInteger::dividedBy() throws when a result is not exact; BigDecimal::dividedBy() requires a scale and optionally a rounding mode; BigRational keeps exact fractions. This forces PMs and finance stakeholders to decide “round half up, half down, down, or up” per use case — before engineering encodes a default. That is a product-governance win, not just a technical one.
Trust boundary decisions for user-entered amounts. The README explicitly warns that of() is for trusted input, while parse() supports syntax restrictions and maxDigits for untrusted strings. This supports a security/product decision: user-supplied prices, quantities, rates, or invoice amounts must be parsed with limits, not passed directly into arithmetic methods. That prevents denial-of-service-style inputs such as extremely large exponents from becoming a production incident.
Roadmap sequencing for a “money-safe” platform. If the roadmap includes subscriptions, marketplaces, multi-currency pricing, payouts, or financial reporting, this package is a near-term foundation rather than a later optimization. It can be introduced behind an internal Money/Amount value object without rewriting product surfaces, then extended as billing complexity grows.
Laravel integration decisions. Use it in Eloquent casts (store as DECIMAL/string, hydrate as BigDecimal), form requests/validation, queued job payloads, and service-layer calculators. Because the objects are immutable and serialization is portable across machines with different PHP extensions, they are suitable for queue workers and distributed Laravel deployments. Avoid exposing BigDecimal directly everywhere; wrap it in domain types so the product can change arithmetic or currency behavior later.
Build-vs-buy boundary at the “money” layer. brick/math is an arithmetic library, not a full money package. It does not provide currency semantics, formatting, allocation across line items, or database persistence. PMs should decide whether to build a thin internal Money abstraction on top of it or adopt a higher-level money package that depends on it. Either way, brick/math is the correct low-level primitive.
Consider brick/math when:
round() calls scattered across controllers and jobs.DECIMAL column comfortably provides at the application layer.parse() with maxDigits and syntax limits) rather than unbounded conversion.0.x release cycle. The README states breaking changes start a new 0.x cycle, so ^0.19-style constraints plus changelog review are required. This is stable enough for production but not semver-stable in the 1.x sense.Look elsewhere or pause when:
brick/math underneath or choose a higher-level package.0.x cycles.brick/math is application-side; large-scale reporting may still need database decimal types, warehouse calculations, or a dedicated ledger design.For executives: This is a small, MIT-licensed, production-used PHP library that reduces the risk of financial calculation errors in billing, invoicing, tax, payouts, and metering. It makes rounding rules explicit and auditable, supports exact decimal and fraction math, and avoids vendor lock-in. Adoption cost is low because it is Composer-installable and can be introduced behind an internal money abstraction. It is not a complete billing system and does not replace finance policy; it is the arithmetic foundation that makes those policies enforceable in code.
For engineering: brick/math provides immutable BigInteger, BigDecimal, and BigRational types with explicit rounding modes, fine-grained exceptions, portable serialization, and optional GMP/BCMath acceleration. It requires PHP 8.2+ and uses a 0.x release model, so pin the release cycle and review changelogs on upgrade. Use parse() with maxDigits for untrusted input, never pass raw user strings into of() or arithmetic methods, and wrap the library in domain value objects rather than letting it leak across the codebase. Avoid floats for monetary values entirely.
For product and finance stakeholders: The key decision this package forces is not “which library,” but “what is our rounding policy per product surface?” Once that is agreed, brick/math gives engineering a precise, testable way to implement it. Expect a modest upfront investment in money types, validation, and persistence conventions; the payoff is fewer reconciliation defects, clearer audit trails, and a safer path to multi-currency, usage-based, or marketplace pricing.
How can I help you explore Laravel packages today?