nickjbedford / decimal
A custom implementation of a Decimal class wrapping the BC library.
1.2.2
2025-02-12 05:05 UTC
Requires
- php: >=8.0
- ext-bcmath: *
Requires (Dev)
README
This library implements the BC Math library into both an immutable and mutable subclass pair of classes named ImmutableDecimal and Decimal (mutable). The classes provide common methods such as simple arithmetic, equality and comparison methods as well as formatting and changes to decimal precision.
Example Usage
Immutable decimal arithmetic
$balance = new \YetAnother\ImmutableDecimal(144.52, 2); $newBalance = $balance->plus(55.95); $newBalance->printValue(); // "200.02" if ($newBalance->greaterThanOrEqual(200)) { print("New balance is at or over $200."); }
Mutable decimal arithmetic (with helpers)
$amount = d4('129.9523'); // 4-digit precision (mutable) Decimal $amount->add(87.5); // addition in place $amount->printValue(); // "217.4523" $remainder = $amount->modulus(5.5); $remainder->printValue(); // "2.9523"