nickjbedford/decimal

A custom implementation of a Decimal class wrapping the BC library.

1.0.4 2024-05-08 02:49 UTC

This package is auto-updated.

Last update: 2024-05-08 02:50:57 UTC


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"