PHPackages                             rtlopez/decimal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rtlopez/decimal

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rtlopez/decimal
===============

An object oriented immutable arbitrary-precision arithmetic library for PHP

1.0.8(4y ago)27262.8k↑14%11[1 PRs](https://github.com/rtlopez/decimal/pulls)2MITPHPPHP &gt;=5.3.0CI failing

Since Jun 6Pushed 4y ago2 watchersCompare

[ Source](https://github.com/rtlopez/decimal)[ Packagist](https://packagist.org/packages/rtlopez/decimal)[ Docs](https://github.com/rtlopez/decimal)[ RSS](/packages/rtlopez-decimal/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (10)Used By (2)

Decimal
=======

[](#decimal)

An object oriented arbitrary precision arithmetic class for PHP (&gt;5.3.3).

[![test](https://github.com/rtlopez/decimal/workflows/test/badge.svg)](https://github.com/rtlopez/decimal/workflows/test/badge.svg)

Installation
------------

[](#installation)

You can use [Composer](http://getcomposer.org/)

To add this package as a local, per-project dependency to your project, simply add a dependency on `rtlopez/decimal` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on `Decimal`:

```
{
  "require": {
    "rtlopez/decimal": "^1.0"
  }
}
```

Features
--------

[](#features)

The `Decimal` class can be used for an arbitrary precision calculation. Main features are:

- Immutable result of each operation,
- The same result precision,
- Safe for finance calculations,
- Fluent interface,
- Many unit test (more than 1100 tests and 3700 assertions),
- Easy to use and install,

Why should I use it?
--------------------

[](#why-should-i-use-it)

The `Decimal` solve several inconveniences known in other solutions.

- `Float` is only an aproximation and have limited precision,
- `Fixed-point` have limited size (64-bits on 64-bit system),
- Pure `bcmath` cannot correctly round, only truncate,
- `GMP` is not object-oriented

Usage
-----

[](#usage)

### Creating objects with simple factory

[](#creating-objects-with-simple-factory)

```
use RtLopez\Decimal;

$n1 = Decimal::create(123, 2);
$n2 = Decimal::create(123.45, 2); // not recommended
$n3 = Decimal::create('123.45', 2);
$n4 = Decimal::create($n1, 2);
```

### Printing and formatting numbers

[](#printing-and-formatting-numbers)

```
$number = Decimal::create('12345.671', 2);

echo (string)$number;
// 12345.67

echo $number->format(1, ',', ' ');
// 12 345,7

echo $number->truncate()->format(null, ',', ' ', false);
// 12 345,00
```

### Arithmetics

[](#arithmetics)

```
$n1 = Decimal::create('12345.671', 3);
$n2 = Decimal::create('11111.111', 3);
$n3 = $n1->add($n2)->mul(-1);
echo $n3; // -23456.782

// fluid interface
$n4 = $n3->pow(2)->sqrt()->mul(-2)->div(2)->abs();
```

### Immutable

[](#immutable)

```
$n1 = Decimal::create('12345.671', 3);
$n1->add(2);
...
echo $n1;
// 12345.671
```

### Comparisions

[](#comparisions)

```
$n1 = Decimal::create('12345.671', 3);
$n2 = Decimal::create('11111.111', 3);
$n1->gt($n2);        // true
$n1->eq(0);          // false
$n2->le(100);        // false
```

### Rounding

[](#rounding)

```
$n = Decimal::create('12345.671', 3);
echo $n->round(2);   // 12345.67
echo $n->round(1);   // 12345.7
echo $n->truncate(); // 12345
echo $n->ceil(1);    // 12345.6
echo $n->floor();    // 12345
```

### Implementations

[](#implementations)

There are exists three internal implementations of this library.

- `RtLopez\DecimalBCMath`: (default)based on bcmath library
- `RtLopez\DecimalFixed`: based on scaled integers
- `RtLopez\DecimalFloat`: based on floats

Only `BCMath` implementation works correctly in wide spectrum of numbers. All others can fail in many border cases. They was implemented only as proof of concept and you can using it only for your own risk.

The default implementation or precision can be changed by functions:

```
Decimal::setDefaultImplementation('RtLopez\DecimalBCMath');
Decimal::setDefaultPrecision(6);
```

Licence
-------

[](#licence)

This library is distributed under MIT Licence.

Issue reporting or feature request
----------------------------------

[](#issue-reporting-or-feature-request)

Coming soon.

TODO
----

[](#todo)

- GMP implementation

Donation
--------

[](#donation)

BTC: 1Lopez7yPtbyjfLGe892JfheDFJMMt43tW LTC: LV3G3sJxz9AYpDMYUp8e1LCmerFYxVY3ak

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 91.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~321 days

Recently: every ~247 days

Total

9

Last Release

1789d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/555798e26509edae3bbf2ffde0751116d3b6e113e73392d11d176cc4cf01be5f?d=identicon)[rtlopez](/maintainers/rtlopez)

---

Top Contributors

[![rtlopez](https://avatars.githubusercontent.com/u/1812055?v=4)](https://github.com/rtlopez "rtlopez (33 commits)")[![ibrunotome](https://avatars.githubusercontent.com/u/4256471?v=4)](https://github.com/ibrunotome "ibrunotome (1 commits)")[![injitools](https://avatars.githubusercontent.com/u/9765406?v=4)](https://github.com/injitools "injitools (1 commits)")[![l3n641](https://avatars.githubusercontent.com/u/14206875?v=4)](https://github.com/l3n641 "l3n641 (1 commits)")

---

Tags

arithmeticdecimalmoneyphpprecisiontestedmoneydecimalbignumimmutablenumbersnumericbcmathgmpfinancialbig decimallarge numbers

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rtlopez-decimal/health.svg)

```
[![Health](https://phpackages.com/badges/rtlopez-decimal/health.svg)](https://phpackages.com/packages/rtlopez-decimal)
```

###  Alternatives

[brick/math

Arbitrary-precision arithmetic library

2.1k504.0M277](/packages/brick-math)[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

22.8k69.3k](/packages/grumpydictator-firefly-iii)[kwn/number-to-words

Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.

4235.0M21](/packages/kwn-number-to-words)[aza/math

AzaMath - Anizoptera CMF mathematic component. Arbitrary precision arithmetic (for huge integers; BCMath wrapper) and universal convertor between positional numeral systems (supported bases from 2 to 62 inclusive, and systems with custom alphabet; pure PHP realisation, can use GMP and core PHP functions for speed optimization).

1921.9k1](/packages/aza-math)[firefly-iii/data-importer

Firefly III Data Import Tool.

7545.8k](/packages/firefly-iii-data-importer)[prestashop/decimal

Object-oriented wrapper/shim for BC Math PHP extension. Allows for arbitrary-precision math operations.

178.2M5](/packages/prestashop-decimal)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
